Elasticsearch 创建索引模板成功后/_index_template接口查询不到对应模板问题
问题根因
Elasticsearch 7.8版本开始引入了V2版索引模板,新旧版本模板的API完全独立不互通:
- 你的Java代码中使用的
PutIndexTemplateRequest属于V1(legacy)索引模板API,创建的旧版模板仅能通过V1接口查询 - 你执行curl调用的
/_index_template是V2模板的专属查询接口,默认不会返回V1版本的模板
排查步骤
- 第一步:确认V1模板是否真实存在,执行以下查询命令:
curl -s http://localhost:9200/_template/ngram-template
如果能正常返回你配置的模板规则,说明模板创建成功,只是查询接口用错。
- 第二步:确认Java代码连接的ES集群和curl请求的集群为同一个,避免跨环境串连的问题。
- 第三步:核对ES服务端版本和Java Rest Client版本的兼容性,避免版本差异导致的API执行异常。
解决方法
方案1:继续使用V1模板
V1模板在目前ES 7.x、8.x版本中仍完全兼容,后续创建匹配规则的索引时会正常生效,仅需更换查询接口为/_template即可。
如果需要查询所有V1模板,执行:
curl -s http://localhost:9200/_template
方案2:迁移到V2模板(推荐,V1模板后续版本会逐步废弃)
将Java代码中的V1 API替换为V2的PutComposableIndexTemplateRequest,示例代码如下:
PutComposableIndexTemplateRequest v2TemplateRequest = new PutComposableIndexTemplateRequest("ngram-template"); ComposableIndexTemplate template = new ComposableIndexTemplate( Arrays.asList("resourcetable-*", "termconcept-*"), // 配置settings,如需配置mappings也可在该map中添加 Collections.singletonMap("settings", Settings.builder().put("index.max_ngram_diff", 50).build()), Collections.emptyList(), // 关联的组件模板,无需求留空即可 100, // 模板优先级,数值越高匹配时越优先 null, // 自定义版本号,可留空 null // 自定义元数据,可留空 ); v2TemplateRequest.indexTemplate(template); AcknowledgedResponse acknowledgedResponse = elasticsearchHighLevelRestClient.indices().putIndexTemplate(v2TemplateRequest, RequestOptions.DEFAULT); assert acknowledgedResponse.isAcknowledged();
修改后调用/_index_template接口即可正常查询到创建的模板。
内容的提问来源于stack exchange,提问作者Jordi
相关产品推荐
相关产品推荐

