﻿---
title: Templates
description: Templates are the mechanism by which Elasticsearch applies settings, mappings, and other configurations when creating indices or data streams. You configure...
url: https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/templates
products:
  - Elasticsearch
applies_to:
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available
---

# Templates
Templates are the mechanism by which Elasticsearch applies settings, mappings, and other configurations when creating indices or data streams.
You configure templates prior to creating indices or data streams. When an index is created, either manually or by indexing a document, the matching template determines the settings, mappings, and other configurations to apply. When used with a [data stream](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/data-streams), a template also defines how each backing index is configured as it is created.
There are two types of template:
- An [**index template**](#index-templates) is the main configuration object applied when creating an index or data stream. It matches index names using `index_patterns` and resolves conflicts using a `priority` value. An index template can optionally define settings, mappings, and aliases directly, and refer to a list of component templates that provide reusable configuration blocks. It can also indicate whether it should create a data stream or a regular index.
- A [**component template**](#component-templates) is a reusable building block that defines settings, mappings, and aliases. Component templates are not applied directly; they must be referenced by index templates.

Together, index templates and their referenced component templates form what is known as *composable templates*.
<tip>
  For a detailed exploration and examples of setting up composable templates, refer to the Elastic blog [Index templating in Elasticsearch: How to use composable templates](https://www.elastic.co/search-labs/blog/index-composable-templates).
</tip>


## Precedence and resolution rules

The following conditions apply to using templates:
- Composable index templates take precedence over any [legacy templates](https://www.elastic.co/guide/en/elasticsearch/reference/8.18/indices-templates-v1.html), which were deprecated in Elasticsearch 7.8. If no composable template matches a given index, a legacy template might 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](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-create) 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.
- When you specify multiple component templates in the `composed_of` field of an index template, the component templates are merged in the order specified, which means that later component templates override earlier component templates.
- If a new data stream or index matches more than one index template, the index template with the highest priority is used.
- When you create an index template, be careful to avoid [naming pattern collisions](#avoid-index-pattern-collisions) with built-in Elasticsearch index templates.


## Index templates

An **index template** is used to configure an index when it is created. [Mappings](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/mapping), [settings](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/index-settings), and [aliases](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/aliases) specified in the index template are inherited by each created index. These can also be specified in the component templates that the index template is composed of.
You can create and manage index templates in Kibana or using the Elasticsearch API.
<tab-set>
  <tab-item title="Kibana">
    <tip>
      If you use Elasticsearch security features, the following [security privileges](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/security-privileges) are required:
      - The `manage_index_templates` cluster privilege to manage index templates.
      To add these privileges, go to **Stack Management > Security > Roles** or use the [Create or update roles](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-security-put-role) API.
    </tip>
    To create an index template, complete the following steps:
    1. Go to the **Index Management** page using the navigation menu or the [global search field](https://www.elastic.co/elastic/docs-builder/docs/3016/explore-analyze/find-and-organize/find-apps-and-objects).
    2. In the **Index Templates** tab, select **Create template**.
       ![Create template page](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/images/elasticsearch-reference-create-template-wizard-my_template.png)
    3. On the **Logistics** page:
       1. Specify a name for the template.
    2. Specify a pattern to match the indices you want to manage with the lifecycle policy. For example, `my-index-*`. Be careful to [avoid naming pattern collisions](#avoid-index-pattern-collisions) with built-in Elasticsearch index templates.
    3. If you're storing continuously generated, append-only data, you can opt to create [data streams](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/data-streams) instead of indices for more efficient storage.
       <note>
       When you enable the data stream option, an option to set **Data retention** also becomes available. Data retention is applicable only if you're using a data stream lifecycle, which is an alternative to index lifecycle management. Refer to the [Data stream lifecycle](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/lifecycle/data-stream) to learn more.
       </note>
    4. Configure any other options you'd like, including:
       - The [index mode](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/index-settings/time-series) to use for the created indices.
    - The template priority, version, and any metadata.
    - Whether or not to overwrite the `action.auto_create_index` cluster setting.
       Refer to the [create or update index template](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-put-index-template) API documentation for details about these options.
    4. On the **Component templates** page, you can use the search and filter tools to select any [component templates](#component-templates) to include in the index template. The index template will inherit the settings (**S**), mappings (**M**), and aliases (**A**) defined in the component templates and apply them to indices when they're created.
    5. On the **Index settings** page, you can define your index settings directly in the index template. When used together with component templates, the mappings defined on this page will override any conflicting definitions from the associated component templates.
       1. Optional: Add any additional [index settings](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/index-settings) that should be applied to the indices as they're created. For example, you can set the number of shards and replicas for each index, as well as configure ILM by specifying [ILM settings](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/configuration-reference/index-lifecycle-management-settings) to apply to the indices:
       ```js
       {
         "index.lifecycle.name": "my_policy",
         "index.lifecycle.rollover_alias": "test-alias",
         "number_of_shards": 1,
         "number_of_replicas": 1
       }
       ```
    6. Optional: On the **Mappings** page, customize the fields and data types used when documents are indexed into Elasticsearch. When used together with component templates, these mappings override any conflicting definitions from the associated component templates.  Refer to [Mapping](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/mapping) for details.
       1. For example, you can define a mapping that contains an [object](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/mapping-reference/object) field named `geo` with a child [`geo_point`](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/mapping-reference/geo-point) field named `coordinates`.
       Alternatively, you can click the **Load JSON** link and define the mapping as JSON:
       ```js
       {
         "properties": {
           "geo": {
             "properties": {
               "coordinates": {
                 "type": "geo_point"
               }
             }
           }
         }
       }
       ```
    7. Optional: On the **Aliases** page, specify an [alias](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/aliases) for each created index. For example, you can define an alias named `my-index`:
       ```js
       {
        "my-index": {}
       }
       ```
       Note that this isn't required when configuring ILM, which instead uses the `index.lifecycle.rollover_alias` setting to access rolling indices.
    8. On the **Review** page, confirm your selections and select **Create template**. You can check your selected options, as well as both the format of the index template that will be created and the associated API request.
    The newly created index template will be used for all new indices with names that match the specified pattern, and for each of these, the specified ILM policy will be applied.
    <dropdown title="Optional: Create new indices to test your template">
      To test your newly created template, you can create two new indices, for example `my-index-000001` and `my-index-000002`.
      1. Index the following documents, to create new indices:
         ```json

         {
           "@timestamp": "2019-05-18T15:57:27.541Z",
           "ip": "225.44.217.191",
           "extension": "jpg",
           "response": "200",
           "geo": {
             "coordinates": {
               "lat": 38.53146222,
               "lon": -121.7864906
             }
           },
           "url": "https://media-for-the-masses.theacademyofperformingartsandscience.org/uploads/charles-fullerton.jpg"
         }


         {
           "@timestamp": "2019-05-20T03:44:20.844Z",
           "ip": "198.247.165.49",
           "extension": "php",
           "response": "200",
           "geo": {
             "coordinates": {
               "lat": 37.13189556,
               "lon": -76.4929875
             }
           },
           "memory": 241720,
           "url": "https://theacademyofperformingartsandscience.org/people/type:astronauts/name:laurel-b-clark/profile"
         }
         ```
      2. Use the [get index API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get) to view the configurations for the new indices. The indices were configured using the index template you created earlier.
         ```json
         ```
    </dropdown>
  </tab-item>

  <tab-item title="API">
    Use the [create or update index template](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-put-index-template) API to add an index template to a cluster.The following request creates an index template that is *composed of* the two component templates shown in the [component templates](#component-templates) example.
    ```json

    {
      "index_patterns": ["te*", "bar*"], <1>
      "template": {
        "settings": {
          "number_of_shards": 1
        },
        "mappings": {
          "_source": {
            "enabled": true
          },
          "properties": {
            "host_name": {
              "type": "keyword"
            },
            "created_at": {
              "type": "date",
              "format": "EEE MMM dd HH:mm:ss Z yyyy"
            }
          }
        },
        "aliases": {
          "mydata": { }
        }
      },
      "priority": 501,
      "composed_of": ["component_template1", "runtime_component_template"],
      "version": 3,
      "_meta": {
        "description": "my custom"
      }
    }
    ```
  </tab-item>
</tab-set>

<tip>
  The following features can be useful when you're setting up index templates:
  - You can test the effect of an index template before putting it into use. Refer to [Simulate multi-component templates](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/templates/simulate-multi-component-templates) to learn more.
  - You can create an index template for a component template that does not yet exist. When doing so, you can use the `ignore_missing_component_templates` configuration option in an index template so that the missing component template is ignored. Refer to [Ignore missing component templates](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/templates/ignore-missing-component-templates) to learn more.
</tip>


### Avoid index pattern collisions

Elasticsearch has built-in index templates, each with a priority of `100`, for the following index patterns:
- `.kibana-reporting*`
- `logs-*-*`
- `metrics-*-*`
- `synthetics-*-*`
- `profiling-*`
- `security_solution-*-*`
- `$.logs`
- `$.logs.*`
- `logs.otel`
- `logs.otel.*`
- `logs.ecs`
- `logs.ecs.*`

[Elastic Agent](https://www.elastic.co/elastic/docs-builder/docs/3016/reference/fleet) 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`](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/configuration-reference/index-management-settings#stack-templates-enabled) to `false` using the [cluster update settings API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-cluster-put-settings). Note, however, that this is not recommended, see the [setting documentation](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/configuration-reference/index-management-settings#stack-templates-enabled) for more information.
- 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 `501`. 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.
- Beginning in Elastic Stack version 9.1, Fleet uses indices named `fleet-synced-integrations*` for a feature. Avoid using this name to avoid collisions with built-in indices.


## Component templates

A **component template** is a reusable building block that defines [mappings](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/mapping), [settings](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/3016/reference/elasticsearch/index-settings), and [aliases](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/data-store/aliases). Component templates are not applied directly to indices, but referenced by [index templates](#index-templates) and used when determining the final configuration of an index.
You can create and manage component templates in Kibana or using the Elasticsearch API.
<tab-set>
  <tab-item title="Kibana">
    You can create, edit, clone, and delete your component templates on the **Index management** page in Kibana. Go to the **Component Templates** tab.
    ![Component templates](https://www.elastic.co/elastic/docs-builder/docs/3016/manage-data/images/serverless-management-component-templates.png)

    - To show details and perform operations, click the template name.
    - To create new component templates, use the **Create component template** wizard.
  </tab-item>

  <tab-item title="API">
    You can create and manage component templates using the [component template](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-cluster-put-component-template) API.
    The following request creates the two component templates used in the previous index template example:
    ```json

    {
      "template": {
        "mappings": {
          "properties": {
            "@timestamp": {
              "type": "date"
            }
          }
        }
      }
    }


    {
      "template": {
        "mappings": {
          "runtime": { <1>
            "day_of_week": {
              "type": "keyword",
              "script": {
                "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"
              }
            }
          }
        }
      }
    }
    ```
  </tab-item>
</tab-set>