官网API说明:
See:Put Mapping API && Get Mapping API
//初始化ES链接:
static RestHighLevelClientrhlclient =new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
Get Mapping API:
GetMappingsRequest request = new GetMappingsRequest();
GetMappingsResponse getMappingResponse = rhlclient.indices().getMapping(request, RequestOptions.DEFAULT);
ImmutableOpenMap> allMappings = getMappingResponse.mappings();
MappingMetaData typeMapping = allMappings.get(INDEX_NAME).get(TYPE_NAME);
//获得Mapping
Map<String, Object> mapping = typeMapping.sourceAsMap();
Put Mapping API:
String json =null;
String fileName = GetElasticSearchMappingsMain.class.getClassLoader().getResource("resourceName").getPath();
json =readJsonData(fileName);
String jsonObject = JSONObject.parseObject(json).toJSONString();
PutMappingRequest mappingRequest =new PutMappingRequest(INDEX_NAME);
mappingRequest.type(TYPE_NAME);
mappingRequest.source(json, XContentType.JSON);
PutMappingResponse putMappingResponse =rhlclient.indices().putMapping(mappingRequest,RequestOptions.DEFAULT);
boolean acknowledged = putMappingResponse.isAcknowledged();