Put index template API

Creates or updates an index template. Index templates define settings, mappings, and aliases that can be applied automatically to new indices.

  1. PUT /_index_template/template_1
  2. {
  3. "index_patterns" : ["te*"],
  4. "priority" : 1,
  5. "template": {
  6. "settings" : {
  7. "number_of_shards" : 2
  8. }
  9. }
  10. }

Request

PUT /_index_template/<index-template>

Description

Elasticsearch applies templates to new indices based on an wildcard pattern that matches the index name.

Index templates are applied during data stream or index creation. For data streams, these settings and mappings are applied when the stream’s backing indices are created.

Settings and mappings specified in a create index request override any settings or mappings specified in an index template.

Changes to index templates do not affect existing indices, including the existing backing indices of a data stream.

Comments in index templates

You can use C-style /* */ block comments in index templates. You can include comments anywhere in the request body, except before the opening curly bracket.

Path parameters

<index-template>

(Required, string) Name of the index template to create.

Query parameters

create

(Optional, boolean) If true, this request cannot replace or update existing index templates. Defaults to false.

master_timeout

(Optional, time units) Specifies the period of time to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.

Request body

index_patterns

(Required, array of strings) Array of wildcard (*) expressions used to match the names of data streams and indices during creation.

Elasticsearch has built-in index templates for the metrics-*-* and logs-*-* index patterns, each with a priority of 100. Elastic Agent uses these templates to create data streams. If you use Elastic Agent, assign your index templates a priority lower than 100 to avoid an overriding the built-in templates.

Otherwise, to avoid accidentally applying the built-in templates, use a non-overlapping index pattern or assign templates with an overlapping pattern a priority higher than 100.

For example, if you don’t use Elastic Agent and want to create a template for the logs-* index pattern, assign your template a priority of 200. This ensures your template is applied instead of the built-in template for logs-*-*.

data_stream

(Optional, object) Indicates whether the template is used to create data streams and their backing indices. If so, use an empty object as the argument:
data_stream: { }.

Data streams require a matching index template with a data_stream object. See Create an index template for a data stream.

template

(Optional, object) Template to be applied. It may optionally include an aliases, mappings, or settings configuration.

Properties of template

composed_of

(Optional, array of strings) An ordered list of component template names. Component templates are merged in the order specified, meaning that the last component template specified has the highest precedence. See Composing multiple component templates for an example.

priority

(Optional, integer) Priority to determine index template precedence when a new data stream or index is created. The index template with the highest priority is chosen. If no priority is specified the template is treated as though it is of priority 0 (lowest priority). This number is not automatically generated by Elasticsearch.

version

(Optional, integer) Version number used to manage index templates externally. This number is not automatically generated by Elasticsearch.

_meta

(Optional, object) Optional user metadata about the index template. May have any contents. This map is not automatically generated by Elasticsearch.

Examples

Index template with index aliases

You can include index aliases in an index template.

  1. PUT _index_template/template_1
  2. {
  3. "index_patterns" : ["te*"],
  4. "template": {
  5. "settings" : {
  6. "number_of_shards" : 1
  7. },
  8. "aliases" : {
  9. "alias1" : {},
  10. "alias2" : {
  11. "filter" : {
  12. "term" : {"user.id" : "kimchy" }
  13. },
  14. "routing" : "shard-1"
  15. },
  16. "{index}-alias" : {}
  17. }
  18. }
  19. }

the {index} placeholder in the alias name will be replaced with the actual index name that the template gets applied to, during index creation.

Multiple matching templates

If multiple index templates match the name of a new index or data stream, the template with the highest priority is used. For example:

  1. PUT /_index_template/template_1
  2. {
  3. "index_patterns" : ["t*"],
  4. "priority" : 0,
  5. "template": {
  6. "settings" : {
  7. "number_of_shards" : 1,
  8. "number_of_replicas": 0
  9. },
  10. "mappings" : {
  11. "_source" : { "enabled" : false }
  12. }
  13. }
  14. }
  15. PUT /_index_template/template_2
  16. {
  17. "index_patterns" : ["te*"],
  18. "priority" : 1,
  19. "template": {
  20. "settings" : {
  21. "number_of_shards" : 2
  22. },
  23. "mappings" : {
  24. "_source" : { "enabled" : true }
  25. }
  26. }
  27. }

For indices that start with te*, _source will enabled, and the index will have two primary shards and one replica, because only template_2 will be applied.

Multiple templates with overlapping index patterns at the same priority are not allowed, and an error will be thrown when attempting to create a template matching an existing index template at identical priorities.

Template versioning

You can use the version parameter to add a version number to an index template. External systems can use these version numbers to simplify template management.

The version parameter is optional and not automatically generated or used by Elasticsearch.

To unset a version, replace the template without specifying one.

  1. PUT /_index_template/template_1
  2. {
  3. "index_patterns" : ["foo", "bar"],
  4. "priority" : 0,
  5. "template": {
  6. "settings" : {
  7. "number_of_shards" : 1
  8. }
  9. },
  10. "version": 123
  11. }

To check the version, you can use the get index template API.

Template metadata

You can use the _meta parameter to add arbitrary metadata to an index template. This user-defined object is stored in the cluster state, so keeping it short is preferrable.

The _meta parameter is optional and not automatically generated or used by Elasticsearch.

To unset _meta, replace the template without specifying one.

  1. PUT /_index_template/template_1
  2. {
  3. "index_patterns": ["foo", "bar"],
  4. "template": {
  5. "settings" : {
  6. "number_of_shards" : 3
  7. }
  8. },
  9. "_meta": {
  10. "description": "set number of shards to three",
  11. "serialization": {
  12. "class": "MyIndexTemplate",
  13. "id": 17
  14. }
  15. }
  16. }

To check the _meta, you can use the get index template API.

Data stream definition

To use an index template for a data stream, the template must include an empty data_stream object. Data stream templates are only used for a stream’s backing indices, they are not applied to regular indices. See Create an index template for a data stream.

  1. PUT /_index_template/template_1
  2. {
  3. "index_patterns": ["logs-*"],
  4. "data_stream": { }
  5. }

Composing aliases, mappings, and settings

When multiple component templates are specified in the composed_of field for an index template, they are merged in the order specified, meaning that later component templates override earlier component templates. Any mappings, settings, or aliases from the parent index template are merged in next. Finally, any configuration on the index request itself is merged.

In this example, the order of the two component templates changes the number of shards for an index:

  1. PUT /_component_template/template_with_2_shards
  2. {
  3. "template": {
  4. "settings": {
  5. "index.number_of_shards": 2
  6. }
  7. }
  8. }
  9. PUT /_component_template/template_with_3_shards
  10. {
  11. "template": {
  12. "settings": {
  13. "index.number_of_shards": 3
  14. }
  15. }
  16. }
  17. PUT /_index_template/template_1
  18. {
  19. "index_patterns": ["t*"],
  20. "composed_of": ["template_with_2_shards", "template_with_3_shards"]
  21. }

In this case, an index matching t* will have three primary shards. If the order of composed templates were reversed, the index would have two primary shards.

Mapping definitions are merged recursively, which means that later mapping components can introduce new field mappings and update the mapping configuration. If a field mapping is already contained in an earlier component, its definition will be completely overwritten by the later one.

This recursive merging strategy applies not only to field mappings, but also root options like dynamic_templates and meta. If an earlier component contains a dynamic_templates block, then by default new dynamic_templates entries are appended onto the end. If an entry already exists with the same key, then it is overwritten by the new definition.