public CreateIndexResponse createIndexWithMapping(String index, Map<String, String> mapping) { CreateIndexResponse response = null; try { CreateIndexRequest request = new CreateIndexRequest(index); request.settings(Settings.builder().put("index.number_of_shards", defaultShards) .put("index.number_of_replicas", defaultReplicas)); request.mapping(generateMapping(mapping)); response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error("创建索引[{}]失败", index, e); } return response; } private XContentBuilder generateMapping(Map<String, String> mapping) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("properties"); for (Map.Entry<String, String> entry : mapping.entrySet()) { { builder.startObject(entry.getKey()); { String dataType=entry.getValue(); if(dataType == null || "".equals(dataType.trim())){ dataType = "String"; } dataType = dataType.toLowerCase(); if("string".equals(dataType)){ dataType="text"; } builder.field("type", dataType); } builder.endObject(); } } builder.endObject(); } builder.endObject(); return builder; }
参考:https://www.cnblogs.com/a-du/p/8807661.html
https://www.zhihu.com/tardis/bd/art/615012104?source_id=1001
标签:index,startObject,java,dataType,builder,request,mapping,动态创建,es From: https://www.cnblogs.com/first131/p/17784734.html