ES7学习日记——Java REST Client索引管理:判断索引存在、获取Settings、获取Mappings

Java REST Client索引管理的一般步骤:

  1. 创建相关请求对象
  2. 执行请求
  3. 查看请求结果

一、判断索引是否存在

        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
        GetIndexRequest exist=new GetIndexRequest("blog");
        boolean exists=client.indices().exists(exist, RequestOptions.DEFAULT);

  如果索引存在,则exists布尔值为真

二、获取Settings

    GetSettingsRequest getSettings=new GetSettingsRequest().indices("blog");
    GetSettingsResponse getSettingsResponse=client.indices().getSettings(getSettings, RequestOptions.DEFAULT);
    String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");

三、获取mappings

        GetMappingsRequest getMappings=new GetMappingsRequest().indices("blog");
        GetMappingsResponse getMappingResponse = client.indices().getMapping(getMappings, RequestOptions.DEFAULT);
        Map<String, MappingMetaData> allMappings = getMappingResponse.mappings(); 
        MappingMetaData indexMapping = allMappings.get("blog"); 
        Map<String, Object> mapping = indexMapping.sourceAsMap();
        Iterator<Entry<String,Object>> entries=mapping.entrySet().iterator();
        while(entries.hasNext()){
            Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key+":"+value);
        }

猜你喜欢

转载自www.cnblogs.com/molihuacha/p/12055169.html