Elasticsearch Java Rest Client创建含composed_of的索引模板报错咨询
问题原因
你用错了Elasticsearch Java High Level Rest Client的请求类:
- 你当前调用的
PutIndexTemplateRequest对应ES遗留索引模板API,路径为/_template/<模板名>,该API本身不支持composed_of参数,因此会抛出未知key的解析错误 - 你要实现的
/_index_template/<模板名>是ES 7.8及以上版本推出的可组合索引模板API,对应的请求类为PutComposableIndexTemplateRequest
解决方法
方式1:使用官方API原生构造(推荐,避免拼写错误)
import org.elasticsearch.client.indices.PutComposableIndexTemplateRequest import org.elasticsearch.common.settings.Settings // 构造可组合索引模板请求 PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest("my_template") // 配置索引匹配规则 request.indexPatterns(List.of("my_pattern_*")) // 配置索引设置 request.settings(Settings.builder().put("number_of_shards", 1).build()) // 配置关联的组件模板 request.composedOf(List.of("composable_index_template1", "composable_index_template2"))
方式2:保留现有JSON拼接逻辑,仅替换请求类
import org.elasticsearch.client.indices.PutComposableIndexTemplateRequest import org.elasticsearch.common.xcontent.XContentType def builder = new groovy.json.JsonBuilder() def root = builder { index_patterns ["my_pattern_*"] template{ settings{ number_of_shards 1 } } composed_of ["composable_index_template1", "composable_index_template2"] } PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest("my_template") request.source(builder.getContent(), XContentType.JSON)
注意事项
- 确保你的ES服务端版本 >=7.8,且Java Rest Client版本和服务端大版本保持一致,避免出现兼容性问题
- 如果需要兼容更低版本的ES,需要放弃使用
composed_of参数,改用遗留模板的嵌套配置逻辑
内容的提问来源于stack exchange,提问作者boraas
相关产品推荐
相关产品推荐

