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官网

  • 相关阅读:
    取得窗口大小和窗口位置兼容所有浏览器的js代码
    一个简单易用的导出Excel类
    如何快速启动chrome插件
    网页表单设计案例
    Ubuntu下的打包解包
    The source file is different from when the module was built. Would you like the debugger to use it anyway?
    FFisher分布
    kalman filter
    Group delay Matlab simulate
    24位位图格式解析
  • 原文地址:https://www.cnblogs.com/bigorang/p/11993791.html
Copyright © 2011-2022 走看看