You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 04:30:02