索引模板容许你定义在建立新索引时自动应用的模板,模板包括设置和映射,以及一个简单的模式模板,该模板控制是否应该将模板应用于新索引。app
模板只在建立索引时应用,更改模板不会对现有索引产生影响,当使用建立索引API时,做为建立索引调用的一部分定义的设置/映射将优先于模板中定义的任何匹配设置/映射。
例如:版本控制
PUT _template/template_1 { "index_patterns": ["te*", "bar*"], "settings": { "number_of_shards": 1 }, "mappings": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "keyword" }, "created_at": { "type": "date", "format": "EEE MMM dd HH:mm:ss Z yyyy" } } } }
索引模板提供c样式
/* */
块注释,在JSON文档中,除了初始左大括号以前,其余地方都容许使用注释。
定义一个名为template_1
的模板,模板模式为te*
或bar*
,设置和映射将应用于任何匹配te*
或bar*
模式的索引名称。code
也能够在索引模板中包含如下别名:orm
PUT _template/template_1 { "index_patterns" : ["te*"], "settings" : { "number_of_shards" : 1 }, "aliases" : { "alias1" : {}, "alias2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" }, "{index}-alias" : {} } }
别名中的
{index}
占位符将被替换为模板在建立索引期间应用到的实际索引名。
索引模板由一个名称标识(在上面的例子中是template_1
),也能够删除:对象
DELETE /_template/template_1
索引模板由一个名称标识(在上面的例子中是template_1
),可使用如下方法检索:索引
GET /_template/template_1
还可使用通配符匹配多个模板,如:文档
GET /_template/temp* GET /_template/template_1,template_2
获取可运行的全部索引模板的列表:io
GET /_template
用于检查模板是否存在,例如:form
HEAD _template/template_1
HTTP状态码指示具备给定名称的模板是否存在,状态码200
表示存在,404
表示不存在。模板
在7.0.0以前,映射定义用于包含类型名称,虽然默认状况下映射再也不包含类型名称,可是仍然能够经过设置参数
include_type_name
使用旧格式。
多个索引模板可能匹配一个索引,在本例中,设置和映射都合并到索引的最终配置中,可使用order
参数控制合并的顺序,先应用较低的顺序,而后用较高的顺序覆盖它们,例如:
PUT /_template/template_1 { "index_patterns" : ["*"], "order" : 0, "settings" : { "number_of_shards" : 1 }, "mappings" : { "_source" : { "enabled" : false } } } PUT /_template/template_2 { "index_patterns" : ["te*"], "order" : 1, "settings" : { "number_of_shards" : 1 }, "mappings" : { "_source" : { "enabled" : true } } }
上面的操做将禁用存储_source
,可是对于以te*
开头的索引,仍然启用_source
,注意,对于映射,合并是“深度”的,这意味着基于对象/属性的映射能够很容易地在高阶模板上添加/覆盖,而低阶模板提供了基础。
具备相同顺序值的多个匹配模板将致使不肯定的合并顺序。
为了简化外部系统对模板的管理,模板能够选择添加一个version
号,版本号能够是任何整数,version
字段是彻底可选的,仅用于模板的外部管理,要取消version
设置,只需替换模板而不用指定另外一个。
PUT /_template/template_1 { "index_patterns" : ["*"], "order" : 0, "settings" : { "number_of_shards" : 1 }, "version": 123 }
要检查版本,可使用filter_path
过滤响应,将响应限制为只有version
:
GET /_template/template_1?filter_path=*.version
这应该会给出一个小的响应,使解析既简单又便宜:
{ "template_1" : { "version" : 123 } }