Elasticsearch NodeJs putIndexTemplate API

Elasticsearch NodeJs putIndexTemplate API

我正在使用 elasticsearch 7.13.3,我想从我的打字稿应用中调用 put index template API。我正在使用包 "@elastic/elasticsearch": "7.13.0" 但我收到调用组合错误。

从 kibana 我可以毫无错误地执行:

PUT _component_template/template-xxx2
{
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "id": {
          "type": "keyword"
        },
        "value": {
          "type": "double",
          "coerce": false
        }
      }
    }
  }
}

PUT _index_template/index-template-xxx2
{
  "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"
  }
}

我想在我的节点应用程序中做同样的事情。 模板创建没问题:

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

但是我找不到 this.db.clientDb.indices.putIndexTemplate({ API 的正确重载。

这给了我错误:(没有重载匹配这个调用)

void this.db.clientDb.indices.putIndexTemplate({
  name: '',
  index_patterns: ["template-xxx2*"], // --> where should I put this property?
  body: {
    settings: {
      number_of_shards: 2
    },
    mappings: {
      _source: {
        enabled: true
      }
    },
    aliases: {
      mydata: {}
    }
  },
  priority: 500,
  composed_of: ['template-xxx2'], // --> where should I put this property?
  version: 3,
  _meta: {
    description: 'template-xxx2 description'
  }
});

我想做this latest script.

索引模板已在 7.8 中进行了大修。以前的旧端点称为 _template,新端点称为 _index_template

您正在混合调用旧端点和新端点,即 putTemplate 调用旧的旧端点,putIndexTemplate 调用新端点。

此外,整个模板定义需要进入body,而不是在调用参数的顶层。

这就是您需要做的。首先,进行此调用以存储组件模板:

void this.db.clientDb.cluster.putComponentTemplate({
    "name": "template-xxx2",
    "body": {
      "template": {
        "mappings": {
          "properties": {
            "@timestamp": {
              "type": "date"
            },
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "double",
              "coerce": false
            }
          }
        }
      }
    }
})

然后使用以下调用存储索引模板:

void this.db.clientDb.indices.putIndexTemplate({
    "name": "index-template-xxx2",
    "body": {
      "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"
      }
    }
})