You need to enable JavaScript to run this app. Elasticsearch 7.x NodeJS客户端putIndexTemplate正确传参方法
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch 7.x NodeJS客户端putIndexTemplate正确传参方法

问题原因

7.13版本的@elastic/elasticsearch客户端中,indices.putIndexTemplate接口仅将name作为顶层路径参数,其余所有和索引模板配置相关的参数(包括index_patterns、composed_of、priority等)都需要统一放在body字段内,同时不要遗漏Kibana请求里的template配置层级。


正确的索引模板创建代码

void this.db.clientDb.indices.putIndexTemplate({
  name: 'index-template-xxx2', // 替换为你的索引模板名称
  body: {
    // Kibana中PUT请求的所有请求体参数都放在该层级下
    index_patterns: ["template-xxx2*"],
    template: {
      settings: {
        number_of_shards: 2
      },
      mappings: {
        _source: {
          enabled: true
        },
        properties: {
          created_at: {
            type: 'date',
            format: "EEE MMM dd HH:mm:ss Z yyyy"
          }
        }
      },
      aliases: {
        mydata: {}
      }
    },
    priority: 600,
    composed_of: ["template-xxx2"],
    version: 3,
    _meta: {
      description: "template-xxx2 description"
    }
  }
})

补充注意

你之前调用的indices.putTemplate是Elasticsearch旧版v1索引模板的接口,和Kibana中PUT _component_template对应的组件模板创建接口为indices.putComponentTemplate,正确的组件模板创建代码如下:

void this.db.clientDb.indices.putComponentTemplate({
  name: `template_${this.index}`,
  body: {
    template: {
      mappings: {
        properties: {
          '@timestamp': {
            type: 'date'
          },
          id: {
            type: 'keyword'
          },
          value: {
            type: 'double',
            coerce: false
          }
        }
      }
    }
  }
})

内容的提问来源于stack exchange,提问作者mene

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 09:24:04