Search code examples
node.jstypescriptelasticsearchelastic-stack

Elasticsearch NodeJs putIndexTemplate API


I'm using elasticsearch 7.13.3 and I want to call the put index template API from my typescript app. I'm using the package "@elastic/elasticsearch": "7.13.0" but I get error for the composition of the call.

From kibana I can execute without any error:

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"
  }
}

and I want do the same from my node app. The template creation it's ok:

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

But I can't find the correct overload for the this.db.clientDb.indices.putIndexTemplate({ API.

This gave me errors: (no overloads match this call)

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'
  }
});

I want to do this latest script.


Solution

  • Index templates have been overhauled in 7.8. The previous legacy endpoint was called _template and the new one is called _index_template.

    You're mixing calls to the old and the new endpoint, i.e. putTemplate calls the old legacy endpoint and putIndexTemplate calls the new one.

    Moreover, the whole template definition needs to go inside body, not at the top level of the call parameters.

    So here is what you need to do. First, make this call to store the component template:

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

    Then store the index template with the following call:

    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"
          }
        }
    })