zoukankan      html  css  js  c++  java
  • es Indices Administration 更新索引

    1.为了访问索引信息,使用adminClient

    IndicesAdminClient indicesAdminClient = client.admin().indices();
    

    2.创建索引

    client.admin().indices().prepareCreate("twitter").get();
    

    3. 设置索引参数

    client.admin().indices().prepareCreate("twitter")     
            .setSettings(Settings.builder()             
                    .put("index.number_of_shards", 3)
                    .put("index.number_of_replicas", 2)
            )
            .get();   
    
    

    4.PUT MAPPING

    client.admin().indices().prepareCreate("twitter")    
            .addMapping("_doc", "message", "type=text") 
            .get();
    Creates an index called twitter
    Add a _doc type with a field called message that has the datatype text.
    

    关于addMapping方法有很多种形式.可以使用 XContentBuilder 或者 Map参数.具体的请参考使用版本的javadoc来选择最适合你的方法。

    PUT mapping也可以用于更新已经创建的索引。如:

    client.admin().indices().preparePutMapping("twitter")   
    .setType("_doc")
    .setSource("{
    " +
            "  "properties": {
    " +
            "    "name": {
    " +                           
            "      "type": "text"
    " +
            "    }
    " +
            "  }
    " +
            "}", XContentType.JSON)
    .get();
    
    // You can also provide the type in the source document
    client.admin().indices().preparePutMapping("twitter")
    .setType("_doc")
    .setSource("{
    " +
            "    "_doc":{
    " +                            
            "        "properties": {
    " +
            "            "name": {
    " +
            "                "type": "text"
    " +
            "            }
    " +
            "        }
    " +
            "    }
    " +
            "}", XContentType.JSON)
    .get();
    
    Puts a mapping on existing index called twitter
    
    Adds a new field name to the mapping
    
    The type can be also provided within the source
    

    5. Refresh刷新

    // 1.刷新所有索引
    client.admin().indices().prepareRefresh().get(); 
     //2.刷新一个索引
    client.admin().indices()
            .prepareRefresh("twitter")               
            .get(); 
     //3. 刷新多个索引
    client.admin().indices()
            .prepareRefresh("twitter", "company")   
            .get(); 
    

    6.获取已存在的索引信息Settings

    GetSettingsResponse response = client.admin().indices()
            .prepareGetSettings("company", "employee").get();                           
    for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) { 
        String index = cursor.key;                                                      
        Settings settings = cursor.value;                                               
        Integer shards = settings.getAsInt("index.number_of_shards", null);             
        Integer replicas = settings.getAsInt("index.number_of_replicas", null);         
    }
    

    Get settings for indices company and employee
    Iterate over results
    Index name
    Settings for the given index
    Number of shards for this index
    Number of replicas for this index

    7.更新索引Settings

    client.admin().indices().prepareUpdateSettings("twitter")   
            .setSettings(Settings.builder()                     
                    .put("index.number_of_replicas", 0)
            )
            .get();
    

    参考链接

    es官网

  • 相关阅读:
    C#与JAVA平台RSA算法交互示例
    .NET_RSA加密全接触(重、难点解析)
    .NET和java的RSA互通,仅此而已
    数据库面试常问的一些基本概念
    亿级Web系统搭建——单机到分布式集群
    (译) JSON-RPC 2.0 规范(中文版)
    有趣的 Mysql 存储引擎
    zend opcache的最佳设置
    PHP注释的艺术——phpDoc规范
    说说$POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别
  • 原文地址:https://www.cnblogs.com/bigorang/p/11993791.html
Copyright © 2011-2022 走看看