Create or update index template

You can use the Create or Update Index Template API to create indexes with predefined mappings and settings as well as update existing index templates.

Path and HTTP methods

  1. PUT _index_template/<template-name>
  2. POST _index_template/<template-name>

Path parameters

ParameterData typeDescription
template-nameStringThe name of the index template.

Query parameters

The following optional query parameters are supported.

ParameterData typeDescription
createBooleanWhen true, the API cannot replace or update any existing index templates. Default is false.
cluster_manager_timeoutTimeThe amount of time to wait for a connection to the cluster manager node. Default is 30s.

Request body options

The following options can be used in the request body to customize the index template.

ParameterTypeDescription
index_patternsString arrayAn array of wildcard expressions that match the names of data streams and indexes created during template creation. Required.
composed_ofString arrayAn ordered list of component template names. These templates are merged using the specified order. For more information, see Using multiple component templates. Optional.
data_streamObjectWhen used, the request creates data streams and any backing indexes based on the template. This setting requires a matching index template. It can also be used with the hidden setting, which, when set to true, hides the data stream backing indexes. Optional.
_metaObjectOptional metadata that provides details about the index template. Optional.
priorityIntegerA number that determines which index templates take precedence during the creation of a new index or data stream. OpenSearch chooses the template with the highest priority. When no priority is given, the template is assigned a 0, signifying the lowest priority. Optional.
templateObjectThe template that includes the aliases, mappings, or settings for the index. For more information, see [#template]. Optional.
versionIntegerThe version number used to manage index templates. Version numbers are not automatically set by OpenSearch. Optional.

Template

You can use the following objects with the template option in the request body.

alias

The name of the alias to associate with the template as a key. Required when the template option exists in the request body. This option supports multiple aliases.

The object body contains the following optional alias parameters.

ParameterData typeDescription
filterQuery DSL objectThe query that limits the number of documents that the alias can access.
index_routingStringThe value that routes indexing operations to a specific shard. When specified, overwrites the routing value for indexing operations.
is_hiddenBooleanWhen true, the alias is hidden. Default is false. All alias indexes must have matching values for this setting.
is_write_indexBooleanWhen true, the index is the write index for the alias. Default is false.
routingStringThe value used to route index and search operations to a specific shard.
search_routingStringThe value used to write specific search operations to a specific shard. When specified, this option overwrites the routing value for search operations.

mappings

The field mappings that exist in the index. For more information, see Mappings and field types. Optional.

settings

Any configuration options for the index. For more information, see Index settings.

Example requests

The following examples show how to use the Create or Update Index Template API.

Index template with index aliases

The following example request includes index aliases in the template:

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

copy

Using multiple matching templates

When multiple index templates match the name of a new index or data stream, the template with the highest priority is used. For example, the following two requests create index templates with different priorities:

  1. PUT /_index_template/template_one
  2. {
  3. "index_patterns" : ["h*"],
  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_two
  16. {
  17. "index_patterns" : ["ha*"],
  18. "priority" : 1,
  19. "template": {
  20. "settings" : {
  21. "number_of_shards" : 2
  22. },
  23. "mappings" : {
  24. "_source" : { "enabled" : true }
  25. }
  26. }
  27. }

copy

For indexes that start with ha, the _source is enabled. Because only template_two is applied, the index will have two primary shards and one replica.

Overlapping index patterns given the same priority are not allowed. An error will occur when attempting to create a template matching an existing index template with identical priorities.

Adding template versioning

The following example request adds a version number to an index template, which simplifies template management for external systems:

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

copy

Adding template metadata

The following example request uses the meta parameter to add metadata to the index template. All metadata is stored in the cluster state:

  1. PUT /_index_template/template_one
  2. {
  3. "index_patterns": ["rom", "juliet"],
  4. "template": {
  5. "settings" : {
  6. "number_of_shards" : 2
  7. }
  8. },
  9. "_meta": {
  10. "description": "Where art thou",
  11. "serialization": {
  12. "class": "MyIndexTemplate",
  13. "id": 12
  14. }
  15. }
  16. }

Data stream definition

Include a data_stream object to use an index template for data streams, as shown in the following example request:

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

Using multiple component templates

When using multiple component templates with the composed_of field, the component templates are merged in the specified order. Next, all mappings, settings, and aliases from the parent index template of the component are merged. Lastly, any configuration options added to the index requests are merged.

In the following example request, an index with h* has two merged primary shards. If the order in the request body were reversed, then the index would have one primary shard:

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

copy

Recursive merging is used for mapping definition and root options such as dynamic_templates and meta, meaning that when an earlier component contains a meta block, new meta entries are added to the end of the metadata in the index. Any entries containing a preexisting key are overwritten.