Elasticsearch Define mapping template for a certain type of index

168 views Asked by At

I wanted to know if there is a way in elasticsearch 8.x to define a mapping template such that whenever an index of say the certain name comes in a mapping is applied to it on the fly.

I have a ES cluster where all my indexes follow the pattern as earnings-*, for example, earnings-2022-11, earnings-2022-12 etc. The mapping i want to apply is for a nested object. Currently, i apply the mapping as follows (manually):

PUT earnings-2022-11/_mapping
{
    "properties": {
      "my_salary": {
        "type": "nested"
      }
  }
}

I looked into the Dynamic Mapping side of things, but I see that these need to be done when I am creating an index, in my case, I am doing a bulk_index creation so I don't think this would work, or I may be wrong.

1

There are 1 answers

0
Val On BEST ANSWER

You need to define an index template:

PUT _index_template/earnings_template
{
  "index_patterns": ["earnings*"],
  "template": {
    "settings": {
      "number_of_shards": 1
    },
    "mappings": {
      "properties": {
        "my_salary": {
          "type": "nested"
        }
      }
    },
    "aliases": {
    }
  },
  "priority": 100,
  "composed_of": [],
}