Index templates

Index templates

This topic describes the composable index templates introduced in Elasticsearch 7.8. For information about how index templates worked previously, see the legacy template documentation.

An index template is a way to tell Elasticsearch how to configure an index when it is created. For data streams, the index template configures the stream’s backing indices as they are created. Templates are configured prior to index creation. When an index is created - either manually or through indexing a document - the template settings are used as a basis for creating the index.

There are two types of templates: index templates and component templates. Component templates are reusable building blocks that configure mappings, settings, and aliases. While you can use component templates to construct index templates, they aren’t directly applied to a set of indices. Index templates can contain a collection of component templates, as well as directly specify settings, mappings, and aliases.

The following conditions apply to index templates:

  • Composable templates take precedence over legacy templates. If no composable template matches a given index, a legacy template may still match and be applied.
  • If an index is created with explicit settings and also matches an index template, the settings from the create index request take precedence over settings specified in the index template and its component templates.
  • Settings specified in the index template itself take precedence over the settings in its component templates.
  • If a new data stream or index matches more than one index template, the index template with the highest priority is used.

Avoid index pattern collisions

Elasticsearch has built-in index templates, each with a priority of 100, for the following index patterns:

  • logs-*-*
  • metrics-*-*
  • synthetics-*-*
  • profiling-*

Elastic Agent uses these templates to create data streams. Index templates created by Fleet integrations use similar overlapping index patterns and have a priority up to 200.

If you use Fleet or Elastic Agent, assign your index templates a priority lower than 100 to avoid overriding these templates. Otherwise, to avoid accidentally applying the templates, do one or more of the following:

  • To disable all built-in index and component templates, set stack.templates.enabled to false using the cluster update settings API.
  • Use a non-overlapping index pattern.
  • Assign templates with an overlapping pattern a priority higher than 500. For example, if you don’t use Fleet or Elastic Agent and want to create a template for the logs-* index pattern, assign your template a priority of 500. This ensures your template is applied instead of the built-in template for logs-*-*.
  • To avoid naming collisions with built-in and Fleet-managed index templates, avoid using @ as part of the name of your own index templates.

Create index template

Use the index template and put component template APIs to create and update index templates. You can also manage index templates from Stack Management in Kibana.

The following requests create two component templates.

  1. resp = client.cluster.put_component_template(
  2. name="component_template1",
  3. template={
  4. "mappings": {
  5. "properties": {
  6. "@timestamp": {
  7. "type": "date"
  8. }
  9. }
  10. }
  11. },
  12. )
  13. print(resp)
  14. resp1 = client.cluster.put_component_template(
  15. name="runtime_component_template",
  16. template={
  17. "mappings": {
  18. "runtime": {
  19. "day_of_week": {
  20. "type": "keyword",
  21. "script": {
  22. "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"
  23. }
  24. }
  25. }
  26. }
  27. },
  28. )
  29. print(resp1)
  1. const response = await client.cluster.putComponentTemplate({
  2. name: "component_template1",
  3. template: {
  4. mappings: {
  5. properties: {
  6. "@timestamp": {
  7. type: "date",
  8. },
  9. },
  10. },
  11. },
  12. });
  13. console.log(response);
  14. const response1 = await client.cluster.putComponentTemplate({
  15. name: "runtime_component_template",
  16. template: {
  17. mappings: {
  18. runtime: {
  19. day_of_week: {
  20. type: "keyword",
  21. script: {
  22. source:
  23. "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))",
  24. },
  25. },
  26. },
  27. },
  28. },
  29. });
  30. console.log(response1);
  1. PUT _component_template/component_template1
  2. {
  3. "template": {
  4. "mappings": {
  5. "properties": {
  6. "@timestamp": {
  7. "type": "date"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. PUT _component_template/runtime_component_template
  14. {
  15. "template": {
  16. "mappings": {
  17. "runtime": {
  18. "day_of_week": {
  19. "type": "keyword",
  20. "script": {
  21. "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }

This component template adds a runtime field named day_of_week to the mappings when a new index matches the template.

The following request creates an index template that is composed of these component templates.

  1. PUT _index_template/template_1
  2. {
  3. "index_patterns": ["te*", "bar*"],
  4. "template": {
  5. "settings": {
  6. "number_of_shards": 1
  7. },
  8. "mappings": {
  9. "_source": {
  10. "enabled": true
  11. },
  12. "properties": {
  13. "host_name": {
  14. "type": "keyword"
  15. },
  16. "created_at": {
  17. "type": "date"
  18. }
  19. }
  20. },
  21. "aliases": {
  22. "mydata": { }
  23. }
  24. },
  25. "priority": 500,
  26. "composed_of": ["component_template1", "runtime_component_template"],
  27. "version": 3,
  28. "_meta": {
  29. "description": "my custom"
  30. }
  31. }