2013-08-28 19:24:34 -04:00
|
|
|
[[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.
|
|
|
|
|
2013-11-26 06:32:25 -05:00
|
|
|
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.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[delete]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Deleting a Template
|
|
|
|
|
|
|
|
Index templates are identified by a name (in the above case
|
2014-03-07 08:21:45 -05:00
|
|
|
`template_1`) and can be deleted as well:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
curl -XDELETE localhost:9200/_template/template_1
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[getting]]
|
2015-05-08 02:05:12 -04:00
|
|
|
=== Getting templates
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-04 14:34:48 -04:00
|
|
|
You can also match several templates by using wildcards like:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
curl -XGET localhost:9200/_template/temp*
|
2013-01-10 11:27:35 -05:00
|
|
|
curl -XGET localhost:9200/_template/template_1,template_2
|
2013-09-04 14:34:48 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-05-02 06:37:02 -04:00
|
|
|
To get list of all index templates you can run:
|
2013-01-10 11:27:35 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
curl -XGET localhost:9200/_template/
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
2014-11-25 10:46:25 -05:00
|
|
|
[float]
|
|
|
|
[[indices-templates-exists]]
|
|
|
|
=== Templates exists
|
|
|
|
|
|
|
|
Used to check if the template exists or not. For example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
-----------------------------------------------
|
2015-03-21 03:54:51 -04:00
|
|
|
curl -XHEAD -i localhost:9200/_template/template_1
|
2014-11-25 10:46:25 -05:00
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[multiple-templates]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== 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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
|
|
|
|
curl -XPUT localhost:9200/_template/template_2 -d '
|
|
|
|
{
|
|
|
|
"template" : "te*",
|
|
|
|
"order" : 1,
|
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"mappings" : {
|
|
|
|
"type1" : {
|
|
|
|
"_source" : { "enabled" : true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
The above will disable storing the `_source` on all `type1` types, but
|
|
|
|
for indices of that start with `te*`, source will still be enabled.
|
|
|
|
Note, for mappings, the merging is "deep", meaning that specific
|
|
|
|
object/property based mappings can easily be added/overridden on higher
|
|
|
|
order templates, with lower order templates providing the basis.
|