<commit_before>[[indices-templates]]
== Index Templates

Index templates allow to define templates that will automatically be
applied to new indices created. The templates include both settings and
mappings, and a simple pattern template that controls if the template
will be applied to the index created. For example:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'
--------------------------------------------------

Defines a template named template_1, with a template pattern of `te*`.
The settings and mappings will be applied to any index name that matches
the `te*` template.

It is also possible to include aliases in an index template as follows:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "aliases" : {
        "alias1" : {},
        "alias2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        },
        "{index}-alias" : {} <1>
    }
}
'
--------------------------------------------------

<1> the `{index}` placeholder within the alias name will be replaced with the
actual index name that the template gets applied to during index creation.

[float]
[[delete]]
=== Deleting a Template

Index templates are identified by a name (in the above case
`template_1`) and can be deleted as well:

[source,js]
--------------------------------------------------
curl -XDELETE localhost:9200/_template/template_1
--------------------------------------------------

[float]
[[getting]]
=== GETting templates

Index templates are identified by a name (in the above case
`template_1`) and can be retrieved using the following:

[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/template_1
--------------------------------------------------

You can also match several templates by using wildcards like:

[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/temp*
curl -XGET localhost:9200/_template/template_1,template_2
--------------------------------------------------

To get list of all index templates you can run:

[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/
--------------------------------------------------


[float]
[[multiple-templates]]
=== Multiple Template Matching

Multiple index templates can potentially match an index, in this case,
both the settings and mappings are merged into the final configuration
of the index. The order of the merging can be controlled using the
`order` parameter, with lower order being applied first, and higher
orders overriding them. For example:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'
<commit_msg>Docs: Adds documentation for indices.exists_template<commit_after>[[indices-templates]]
== Index Templates

Index templates allow to define templates that will automatically be
applied to new indices created. The templates include both settings and
mappings, and a simple pattern template that controls if the template
will be applied to the index created. For example:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'
--------------------------------------------------

Defines a template named template_1, with a template pattern of `te*`.
The settings and mappings will be applied to any index name that matches
the `te*` template.

It is also possible to include aliases in an index template as follows:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "aliases" : {
        "alias1" : {},
        "alias2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        },
        "{index}-alias" : {} <1>
    }
}
'
--------------------------------------------------

<1> the `{index}` placeholder within the alias name will be replaced with the
actual index name that the template gets applied to during index creation.

[float]
[[delete]]
=== Deleting a Template

Index templates are identified by a name (in the above case
`template_1`) and can be deleted as well:

[source,js]
--------------------------------------------------
curl -XDELETE localhost:9200/_template/template_1
--------------------------------------------------

[float]
[[getting]]
=== GETting templates

Index templates are identified by a name (in the above case
`template_1`) and can be retrieved using the following:

[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/template_1
--------------------------------------------------

You can also match several templates by using wildcards like:

[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/temp*
curl -XGET localhost:9200/_template/template_1,template_2
--------------------------------------------------

To get list of all index templates you can run:

[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/
--------------------------------------------------


[float]
[[indices-templates-exists]]
=== Templates exists

Used to check if the template exists or not. For example:

[source,js]
-----------------------------------------------
curl -XHEAD localhost:9200/_template/template_1
-----------------------------------------------

The HTTP status code indicates if the template with the given name
exists or not. A status code `200` means it exists, a `404` it does not.


[float]
[[multiple-templates]]
=== Multiple Template Matching

Multiple index templates can potentially match an index, in this case,
both the settings and mappings are merged into the final configuration
of the index. The order of the merging can be controlled using the
`order` parameter, with lower order being applied first, and higher
orders overriding them. For example:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'

{'fraction_non_alphanumeric': 0.27692948362021097, 'fraction_numerical': 0.015963353692393115, 'mean_word_length': 1.5732142857142857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '400072', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1769, 'n_words': 652}