2013-08-28 19:24:34 -04:00
|
|
|
[[indices-templates]]
|
2019-07-19 14:35:36 -04:00
|
|
|
=== Index Templates
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-08-24 08:59:03 -04:00
|
|
|
Index templates allow you to define templates that will automatically be
|
2017-08-08 05:18:31 -04:00
|
|
|
applied when new indices are created. The templates include both
|
|
|
|
<<index-modules-settings,settings>> and <<mapping,mappings>>
|
|
|
|
and a simple pattern template that controls whether the template should be
|
2016-11-10 18:00:30 -05:00
|
|
|
applied to the new index.
|
2016-03-31 05:15:01 -04:00
|
|
|
|
2017-10-12 17:52:43 -04:00
|
|
|
NOTE: Templates are only applied at index creation time. Changing a template
|
|
|
|
will have no impact on existing indices. When using the create index API, the
|
|
|
|
settings/mappings defined as part of the create index call will take precedence
|
|
|
|
over any matching settings/mappings defined in the template.
|
2016-03-31 05:15:01 -04:00
|
|
|
|
|
|
|
For example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-04-29 10:42:03 -04:00
|
|
|
PUT _template/template_1
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-11-10 18:00:30 -05:00
|
|
|
"index_patterns": ["te*", "bar*"],
|
2016-04-04 13:29:07 -04:00
|
|
|
"settings": {
|
|
|
|
"number_of_shards": 1
|
|
|
|
},
|
|
|
|
"mappings": {
|
2019-01-14 16:08:01 -05:00
|
|
|
"_source": {
|
|
|
|
"enabled": false
|
|
|
|
},
|
|
|
|
"properties": {
|
|
|
|
"host_name": {
|
|
|
|
"type": "keyword"
|
2016-04-04 13:29:07 -04:00
|
|
|
},
|
2019-01-14 16:08:01 -05:00
|
|
|
"created_at": {
|
|
|
|
"type": "date",
|
|
|
|
"format": "EEE MMM dd HH:mm:ss Z yyyy"
|
2016-04-04 13:29:07 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2016-04-04 13:29:07 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-09-02 18:22:30 -04:00
|
|
|
// TESTSETUP
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2016-10-08 06:38:45 -04:00
|
|
|
NOTE: Index templates provide C-style /* */ block comments. Comments are allowed
|
2016-11-18 10:55:04 -05:00
|
|
|
everywhere in the JSON document except before the initial opening curly bracket.
|
2016-10-08 06:38:45 -04:00
|
|
|
|
2018-10-25 16:51:18 -04:00
|
|
|
Defines a template named `template_1`, with a template pattern of `te*` or `bar*`.
|
2013-08-28 19:24:34 -04:00
|
|
|
The settings and mappings will be applied to any index name that matches
|
2018-10-25 16:51:18 -04:00
|
|
|
the `te*` or `bar*` pattern.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-11-26 06:32:25 -05:00
|
|
|
It is also possible to include aliases in an index template as follows:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
PUT _template/template_1
|
2013-11-26 06:32:25 -05:00
|
|
|
{
|
2016-11-10 18:00:30 -05:00
|
|
|
"index_patterns" : ["te*"],
|
2013-11-26 06:32:25 -05:00
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"aliases" : {
|
|
|
|
"alias1" : {},
|
|
|
|
"alias2" : {
|
|
|
|
"filter" : {
|
|
|
|
"term" : {"user" : "kimchy" }
|
|
|
|
},
|
|
|
|
"routing" : "kimchy"
|
|
|
|
},
|
|
|
|
"{index}-alias" : {} <1>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[s/^/DELETE _template\/template_1\n/]
|
2013-11-26 06:32:25 -05:00
|
|
|
|
2016-11-18 10:55:04 -05:00
|
|
|
<1> the `{index}` placeholder in the alias name will be replaced with the
|
|
|
|
actual index name that the template gets applied to, during index creation.
|
2013-11-26 06:32:25 -05:00
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[delete]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Deleting a Template
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
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]
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
DELETE /_template/template_1
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[getting]]
|
2019-07-19 14:35:36 -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]
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
GET /_template/template_1
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-04 14:34:48 -04:00
|
|
|
You can also match several templates by using wildcards like:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
GET /_template/temp*
|
|
|
|
GET /_template/template_1,template_2
|
2013-09-04 14:34:48 -04:00
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
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]
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
GET /_template
|
2013-01-10 11:27:35 -05:00
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
2013-01-10 11:27:35 -05:00
|
|
|
|
2014-11-25 10:46:25 -05:00
|
|
|
[float]
|
|
|
|
[[indices-templates-exists]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Template exists
|
2014-11-25 10:46:25 -05:00
|
|
|
|
|
|
|
Used to check if the template exists or not. For example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
-----------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
HEAD _template/template_1
|
2014-11-25 10:46:25 -05:00
|
|
|
-----------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
2014-11-25 10:46:25 -05:00
|
|
|
|
|
|
|
The HTTP status code indicates if the template with the given name
|
2016-11-18 10:55:04 -05:00
|
|
|
exists or not. Status code `200` means it exists and `404` means
|
|
|
|
it does not.
|
2014-11-25 10:46:25 -05:00
|
|
|
|
2019-01-14 16:08:01 -05:00
|
|
|
NOTE: Before 7.0.0, the 'mappings' definition used to include a type name. Although mappings
|
|
|
|
no longer contain a type name by default, you can still use the old format by setting
|
|
|
|
the parameter include_type_name. For more details, please see <<removal-of-types>>.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[multiple-templates]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Multiple Templates Matching
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
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]
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
PUT /_template/template_1
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-11-10 18:00:30 -05:00
|
|
|
"index_patterns" : ["*"],
|
2013-08-28 19:24:34 -04:00
|
|
|
"order" : 0,
|
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"mappings" : {
|
2019-01-14 16:08:01 -05:00
|
|
|
"_source" : { "enabled" : false }
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 18:22:30 -04:00
|
|
|
PUT /_template/template_2
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-11-10 18:00:30 -05:00
|
|
|
"index_patterns" : ["te*"],
|
2013-08-28 19:24:34 -04:00
|
|
|
"order" : 1,
|
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"mappings" : {
|
2019-01-14 16:08:01 -05:00
|
|
|
"_source" : { "enabled" : true }
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-02 18:22:30 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[s/^/DELETE _template\/template_1\n/]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2018-06-04 09:42:29 -04:00
|
|
|
The above will disable storing the `_source`, but
|
2016-11-18 10:55:04 -05:00
|
|
|
for indices that start with `te*`, `_source` will still be enabled.
|
2013-08-28 19:24:34 -04:00
|
|
|
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.
|
2016-10-18 11:56:18 -04:00
|
|
|
|
2018-11-29 23:56:32 -05:00
|
|
|
NOTE: Multiple matching templates with the same order value will
|
|
|
|
result in a non-deterministic merging order.
|
|
|
|
|
2016-10-18 11:56:18 -04:00
|
|
|
[float]
|
|
|
|
[[versioning-templates]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Template Versioning
|
2016-10-18 11:56:18 -04:00
|
|
|
|
|
|
|
Templates can optionally add a `version` number, which can be any integer value,
|
|
|
|
in order to simplify template management by external systems. The `version`
|
|
|
|
field is completely optional and it is meant solely for external management of
|
|
|
|
templates. To unset a `version`, simply replace the template without specifying
|
|
|
|
one.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
PUT /_template/template_1
|
|
|
|
{
|
2016-11-10 18:00:30 -05:00
|
|
|
"index_patterns" : ["*"],
|
2016-10-18 11:56:18 -04:00
|
|
|
"order" : 0,
|
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"version": 123
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
|
2016-11-18 10:55:04 -05:00
|
|
|
To check the `version`, you can
|
2016-10-18 11:56:18 -04:00
|
|
|
<<common-options-response-filtering, filter responses>>
|
|
|
|
using `filter_path` to limit the response to just the `version`:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
GET /_template/template_1?filter_path=*.version
|
|
|
|
--------------------------------------------------
|
2016-10-18 12:57:03 -04:00
|
|
|
// CONSOLE
|
2016-10-18 11:56:18 -04:00
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
This should give a small response that makes it both easy and inexpensive to parse:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"template_1" : {
|
|
|
|
"version" : 123
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-10-18 12:57:03 -04:00
|
|
|
// TESTRESPONSE
|