Create or update component template

You can use the Component Template API to create or update a component template. A component template is a reusable building block that defines settings, mappings, and aliases that can be shared across multiple index templates.

An index template can be constructed using multiple component templates. To incorporate a component template into an index template, you need to list it in the composed_of section of the index template. Component templates are only applied to newly created data streams and indexes that match the criteria specified in the index template.

If any settings or mappings are directly defined in the index template or the index creation request, those settings will take precedence over the settings or mappings specified in a component template.

Component templates are used solely during the process of index creation. For data streams, this includes the creation of the data stream itself and the creation of the backing indexes that support the stream. Modifications made to component templates will not affect existing indexes, including the backing indexes of a data stream.

Path and HTTP methods

The PUT method adds a component template and accepts both query parameters and a request body. The GET method retrieves information about an existing component template and accepts only query parameters:

  1. PUT _component_template/<component-template-name>
  2. GET _component_template/<component-template-name>

Path parameters

ParameterData typeDescription
component-template-nameStringThe name of the component 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.
timeoutTimeThe amount of time for the operation to wait for a response. Default is 30s.

Request body fields

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

ParameterData typeDescription
templateObjectThe template that includes the aliases, mappings, or settings for the index. For more information, see [#template]. Required.
versionIntegerThe version number used to manage index templates. Version numbers are not automatically set by OpenSearch. Optional.
_metaObjectThe metadata that provides details about the index template. Optional.
allow_auto_createBooleanWhen true, indexes can be automatically created with this template even if the actions.auto_create_index is disabled. When false, indexes and data streams matching the template cannot be automatically created. Optional.
deprecatedBooleanWhen true, the component template is deprecated. If deprecated, OpenSearch will output a warning whenever the template is referenced.

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 the indexing operations to a specific shard. When specified, overwrites the routing value for the 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 search operations to a specific shard. When specified, this option overwrites the routing value for the 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 example requests show how to use the Component Template API.

Create with index aliases

The following example request creates a component template including index aliases:

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

Adding component versioning

The following example adds a version number to a component template which simplifies template management for external systems:

  1. PUT /_component_template/version_template
  2. {
  3. "template": {
  4. "settings" : {
  5. "number_of_shards" : 1
  6. }
  7. },
  8. "version": 3
  9. }

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 /_component_template/meta_template
  2. {
  3. "template": {
  4. "settings" : {
  5. "number_of_shards" : 1
  6. }
  7. },
  8. "_meta": {
  9. "description": "Where art thou",
  10. "serialization": {
  11. "class": "MyIndexTemplate",
  12. "id": 12
  13. }
  14. }
  15. }