Java REST Client索引管理的一般步驟:
- 創建相關請求對象
- 執行請求
- 查看請求結果
一、判斷索引是否存在
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); }