Put component template API

Creates or updates a component template. Component templates are building blocks for constructing index templates. that specify index mappings, settings, and aliases.

  1. PUT _component_template/template_1
  2. {
  3. "template": {
  4. "settings": {
  5. "number_of_shards": 1
  6. },
  7. "mappings": {
  8. "_source": {
  9. "enabled": false
  10. },
  11. "properties": {
  12. "host_name": {
  13. "type": "keyword"
  14. },
  15. "created_at": {
  16. "type": "date",
  17. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  18. }
  19. }
  20. }
  21. }
  22. }

Request

PUT /_component_template/<component-template>

Description

An index template can be composed of multiple component templates. To use a component template, specify it in an index template’s composed_of list. Component templates are only applied to new data streams and indices as part of a matching index template.

Settings and mappings specified directly in the index template or the create index request override any settings or mappings specified in a component template.

Component templates are only used during index creation. For data streams, this includes data stream creation and the creation of a stream’s backing indices. Changes to component templates do not affect existing indices, including a stream’s backing indices.

Comments in component templates

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

Path parameters

<component-template>

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

Query parameters

create

(Optional, boolean) If true, this request cannot replace or update existing component 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

template

(Required, object) This is the template to be applied, may optionally include a mappings, settings, or aliases configuration.

aliases

(Optional, alias object) Index aliases which include the index. See Update index alias.

You cannot add data streams to an index alias.

mappings

(Optional, mapping object) Mapping for fields in the index. If specified, this mapping can include:

See Mapping.

settings

(Optional, index setting object) Configuration options for the index. See Index Settings.

version

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

_meta

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

Examples

Component template with index aliases

You can include index aliases in a component template.

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

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

Applying component templates

You cannot directly apply a component template to a data stream or index. To be applied, a component template must be included in an index template’s composed_of list. See Index templates .

Component template versioning

You can use the version parameter to add a version number to a component 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 /_component_template/template_1
  2. {
  3. "template": {
  4. "settings" : {
  5. "number_of_shards" : 1
  6. }
  7. },
  8. "version": 123
  9. }

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

Component template metadata

You can use the _meta parameter to add arbitrary metadata to a component 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 /_component_template/template_1
  2. {
  3. "template": {
  4. "settings" : {
  5. "number_of_shards" : 1
  6. }
  7. },
  8. "_meta": {
  9. "description": "set number of shards to one",
  10. "serialization": {
  11. "class": "MyComponentTemplate",
  12. "id": 10
  13. }
  14. }
  15. }

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