Elasticsearch2.3.4升级7.10.0后GetMappings接口参数取值问题
Elasticsearch 7.10.0 GetMappings 用法解答
结论
你最后一行代码mappings().get()的入参应该填索引名index1,无需再传入类型名type1。
原因说明
- Elasticsearch 从7.0版本开始正式废弃了自定义索引类型(type),单索引下仅支持一个隐式的默认类型
_doc,原有多type的层级结构被移除。 - 2.3.4版本的返回结构是
索引名 -> 类型名 -> mapping源的两层Map结构,所以需要先取索引再取类型;7.10.0的GetMappingsResponse#mappings()返回的是Map<String, MappingMetadata>结构,key为索引名,对应的value直接就是该索引的完整mapping元数据。
修正后的完整代码
GetMappingsRequest mappingsRequest = new GetMappingsRequest(); mappingsRequest.indices("index1"); GetMappingsResponse mappingsResponse = client.indices().getMapping(mappingsRequest, RequestOptions.DEFAULT); Map<String, Object> mappingMeta = mappingsResponse.mappings().get("index1").getSourceAsMap();
兼容提示
如果你是从2.3.4版本直接升级上来,索引还保留了旧的type1兼容配置,获取到的mapping元数据里也会包含原有type下的所有字段定义,无需额外处理。
内容的提问来源于stack exchange,提问作者Devkinandan Chauhan
相关产品推荐
相关产品推荐

