ES升级至7.10.0后如何通过高级REST客户端获取指定type的mapping
Elasticsearch 自7.x版本开始默认移除了单索引多type的支持,默认单个索引仅存在一个_doc内置type,但对于从低版本迁移而来、开启了兼容配置的索引,仍然可以通过以下两种方式获取指定type的mapping:
方案1:请求时直接指定查询type(兼容旧版本推荐)
在构造GetMappingsRequest时通过types方法指定需要查询的type名称,返回结果会直接返回对应type的mapping配置:
GetMappingsRequest mappingsRequest = new GetMappingsRequest(); mappingsRequest.indices("index1"); // 指定要查询的type名称 mappingsRequest.types("type1"); GetMappingsResponse mappingsResponse = client.indices().getMapping(mappingsRequest, RequestOptions.DEFAULT); Map<String, Object> type1MappingMeta = mappingsResponse.mappings().get("index1").getSourceAsMap();
方案2:全索引mapping中手动提取
如果不调整请求参数,也可以在拿到全索引的mapping配置后,以type名称为key手动提取对应配置:
GetMappingsRequest mappingsRequest = new GetMappingsRequest(); mappingsRequest.indices("index1"); GetMappingsResponse mappingsResponse = client.indices().getMapping(mappingsRequest, RequestOptions.DEFAULT); Map<String, Object> indexAllMapping = mappingsResponse.mappings().get("index1").getSourceAsMap(); // 以type名称为key提取对应mapping Map<String, Object> type1MappingMeta = (Map<String, Object>) indexAllMapping.get("type1");
注意事项
- 若索引为7.x及以上版本新创建,本身不存在自定义type,默认获取的全索引mapping即为内置
_doc对应的配置,无需额外提取。 types相关API在7.x版本已被标记为废弃,8.x版本会完全移除,后续如果不再兼容低版本旧索引,建议调整业务逻辑放弃多type使用,遵循单索引对应单一数据类型的规范。
内容的提问来源于stack exchange,提问作者Devkinandan Chauhan
相关产品推荐
相关产品推荐

