Elasticsearch Java API创建索引后Mappings字段为空问题求助
问题描述
使用Java API创建Elasticsearch(6.8.4版本)索引后,通过curl查询发现mappings字段为空,curl查询结果如下:
# curl -XGET localhost:9200/initest2 {"initest2":{"aliases":{},"mappings":{},"settings":{"index":{"keyspace":"keyspace_test","number_of_shards":"5","provided_name":"initest2","max_result_window":"10000000","creation_date":"1671025346350","analysis":{"normalizer":{"testnormalizer":{"filter":["asciifolding"],"type":"custom","char_filter":[]}}},"number_of_replicas":"0","uuid":"BK9oTTKETZeWqiuR_tGd8w","version":{"created":"6080499"}}}}}
对应的Java代码如下:
String indexQuery = ""; StringBuilder indexString = new StringBuilder(""); String test = "{\"settings\": {\"max_result_window\":10000000,\"keyspace\":\""+keyspaceName+'\u0022' +",\"analysis\": " + "{\"normalizer\":{\"testnormalizer\": {\"type\": \"custom\",\"char_filter\": [],\"filter\": [\"asciifolding\"]}}}}, + \"mappings\":{\""+tableName+"\":{\"discover\" : \" .*\",\"properties\":{"; indexString.append(test); for (String i : indexColumns) { indexString.append('\u0022'+i+'\u0022' +":" +"{ \"type\": \"keyword\",\"normalizer\": \"testnormalizer\"}" + ","); } indexString.delete(indexString.length()-1, indexString.length()).append("}}}}"); indexQuery = indexString.toString(); logger.info("index query is " + indexQuery); CreateIndexRequest request = new CreateIndexRequest(tableName); request.source(indexQuery, XContentType.JSON); CreateIndexResponse createIndexResponse= restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
调用RestHighLevelClient前打印的索引请求内容:
{"settings": {"max_result_window":10000000,"keyspace":"ndl","analysis": {"normalizer":{"testnormalizer": {"type": "custom","char_filter": [],"filter": ["asciifolding"]}}}},"mappings":{"initest2":{"discover" : ".*","properties":{"name":{ "type": "keyword","normalizer": "testnormalizer"}}}}}
已尝试使用request.settings()和request.mappings()方法但未解决问题,需要Java代码修改方案或Elassandra配置调整建议。
解决方案
一、Java代码修改方案
1. 适配ES 6.x的mappings格式
ES 6.x版本不支持直接将索引名作为类型名嵌套在mappings下,需使用默认类型_doc(或对应Cassandra表名作为类型,需与Elassandra配置匹配),同时修正原请求中discover字段的多余空格:
推荐使用ES官方API构建器替代手动拼接JSON,避免语法错误:
// 构建settings Settings settings = Settings.builder() .put("max_result_window", 10000000) .put("keyspace", keyspaceName) .put("analysis.normalizer.testnormalizer.type", "custom") .putArray("analysis.normalizer.testnormalizer.filter", "asciifolding") .build(); // 构建mappings XContentBuilder mappingsBuilder = XContentFactory.jsonBuilder(); mappingsBuilder.startObject() .startObject("_doc") // ES 6.x默认类型 .field("discover", ".*") .startObject("properties"); for (String col : indexColumns) { mappingsBuilder.startObject(col) .field("type", "keyword") .field("normalizer", "testnormalizer") .endObject(); } mappingsBuilder.endObject() // 闭合properties .endObject() // 闭合_doc .endObject(); // 创建索引请求 CreateIndexRequest request = new CreateIndexRequest(tableName); request.settings(settings); request.mapping("_doc", mappingsBuilder); CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
2. 排查手动JSON拼接隐患
原代码手动拼接JSON易出现隐形语法错误(如引号、逗号拼接失误),使用API构建器可彻底规避这类问题,同时便于调试和维护。
二、Elassandra配置调整建议
- 检查
elasticsearch.yaml核心配置:- 确保
elassandra.auto_create_mapping设置为true,允许自动创建映射 - 确认
keyspace已在Cassandra中创建,且Elassandra服务有访问权限
- 确保
- 查看Elassandra日志:
- 检查创建索引时的错误日志,排查权限不足、映射格式不兼容等问题
- 版本兼容性:
- 确保Elassandra版本为
6.8.4.x系列,与ES 6.8.4完全匹配,版本不匹配会导致映射无法生效
- 确保Elassandra版本为
内容的提问来源于stack exchange,提问作者mpsimham
相关产品推荐
相关产品推荐

