index template
index模板是什么
我们知道,向es中的每个index写入数据的时候,数据是有mapping的, 而这个mapping可以是dynamic mapping,也可以是事先创建的mapping,如果使用自动mapping的时候可能会出现问题,比如一个doc中有个字段值为2018-06-06
写入ES的时候会被映射成date类型,另一个doc的同样字段的值为aaa
这时候就会报错,这时候可以通过手动指定mapping来解决问题,但是公司的业务是每天会创建一个index, 如果手动指定mapping的话,这时候维护管理这些Index就会复杂,如果有一种机制,不管创建什么index,都会按照默认设置的mapping去映射字段多好啊,当然,ES 是提供这样的机制的,就是index template
索引模板允许您定义创建新索引时将自动应用的模板。 模板包括设置和映射,以及一个简单的模板模板,用于控制模板是否应用于新索引。
使用案例
实例1:创建Index template
PUT _template/template_1
{
"template": "te*",
"settings": {
"number_of_shards": 1
},
"mappings": {
"type1": {
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "EEE MMM dd HH:mm:ss Z YYYY"
}
}
}
}
}
说明:
创建了一个以te开头的index名称,type为type1这样的模板,该模板有2个字段: host_name
,created_at
DELETE _template/template_1
PUT _template/template_1
{
"index_patterns" : ["te*"],
"settings" : {
"number_of_shards" : 1
},
"aliases" : {
"alias1" : {},
"alias2" : {
"filter" : {
"term" : {"user" : "kimchy" }
},
"routing" : "kimchy"
},
"{index}-alias" : {}
}
}
PUT test/_doc/1
{
"user":"kimchy",
"age":20
}
PUT test/_doc/2
{
"user":"jacky",
"age":21
}
PUT test/_doc/3
{
"user":"ken",
"age":33
}
GET test/_mapping
删除模板
DELETE /_template/template_1
获取模板
- 获取单个模板
GET /_template/template_1
- 获取多个模板
GET /_template/temp*
GET /_template/template_1,template_2
- 获取所有模板
GET /_template
判断模板是否存在
HEAD _template/template_1
多模板匹配
多个索引模板可能会与一个索引匹配,在这种情况下,设置和映射都会合并到索引的最终配置中。 可以使用order参数来控制合并的顺序,首先应用较低的order,并使用较高的order覆盖它们。 例如:
PUT /_template/template_1
{
"index_patterns" : ["*"],
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : false }
}
}
}
PUT /_template/template_2
{
"index_patterns" : ["te*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : true }
}
}
}
说明:
以上将禁止在所有type1类型中存储_source,但对于以te *开头的索引,_source仍将被启用。 请注意,对于映射,合并是“深度”的,这意味着基于特定对象/属性的映射可以轻松地添加到高阶模板上,也可以使用低阶模板提供基础。
模板版本
为了简化外部系统的模板管理,模板可以选择添加版本号,该版本号可以是任何整数值。 版本字段是完全可选的,仅用于模板的外部管理。 要取消设置版本,只需替换模板而不指定一个。
PUT /_template/template_1
{
"index_patterns" : ["*"],
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"version": 123
}
要检查版本,可以使用filter_path过滤响应以将响应限制为仅版本:
GET /_template/template_1?filter_path=*.version
两个较为复杂的template实例
设置全局默认的index template
- kibana中操作
PUT _template/default_global
{
"template":"*",
"settings":{
"index":{
"routing":{
"allocation":{
"require":{
"box_type":"warm"
}
}
},
"refresh_interval":"2s",
"number_of_shards":"5",
"translog":{
"sync_interval":"60s",
"durability":"async"
},
"number_of_replicas":"1"
}
},
"mappings":{
"_default_":{
"dynamic_templates":[
{
"message_field":{
"path_match":"@message",
"mapping":{
"norms":false,
"type":"text"
},
"match_mapping_type":"string"
}
},
{
"string_fields":{
"mapping":{
"type":"keyword",
"fields":{
"smart":{
"norms":false,
"type":"text"
}
}
},
"match_mapping_type":"string",
"match":"*"
}
}
],
"properties":{
"@timestamp":{
"type":"date"
}
}
}
},
"aliases":{
}
}
- curl 命令操作:
curl -XPUT ip:9200/_template/default_global -d '
{
"template":"*",
"settings":{
"index":{
"routing":{
"allocation":{
"require":{
"box_type":"warm"
}
}
},
"refresh_interval":"2s",
"number_of_shards":"5",
"translog":{
"sync_interval":"60s",
"durability":"async"
},
"number_of_replicas":"1"
}
},
"mappings":{
"_default_":{
"dynamic_templates":[
{
"message_field":{
"path_match":"@message",
"mapping":{
"norms":false,
"type":"text"
},
"match_mapping_type":"string"
}
},
{
"string_fields":{
"mapping":{
"type":"keyword",
"fields":{
"smart":{
"norms":false,
"type":"text"
}
}
},
"match_mapping_type":"string",
"match":"*"
}
}
],
"properties":{
"@timestamp":{
"type":"date"
}
}
}
},
"aliases":{
}
}
'
查看模板定义如下:
GET /_template/default_global
{
"default_global":{
"order":0,
"template":"*",
"settings":{
"index":{
"routing":{
"allocation":{ "require":{ "box_type":"warm" } } },
"refresh_interval":"2s",
"number_of_shards":"5",
"translog":{
"sync_interval":"60s",
"durability":"async" },
"number_of_replicas":"1"
}
},
"mappings":{
"_default_":{
"dynamic_templates":[
{
"message_field":{ "path_match":"@message", "mapping":{ "norms":false, "type":"text" }, "match_mapping_type":"string" } },
{
"string_fields":{ "mapping":{ "type":"keyword", "fields":{ "smart":{ "norms":false, "type":"text" } } }, "match_mapping_type":"string", "match":"*" } }
],
"properties":{
"@timestamp":{ "type":"date" } }
}
},
"aliases":{
}
}
}
说明:
setting中的 routing中的设置 allocation.require.box_type=warm ,这里设置了创建index的时候在warm节点上创建,这时候要求es集群中必须配置每个节点的标记来标识每个节点的状态,比如是warm的还是hot的,如果es集群没有配置,index是无法创建的
es6.2.4中index template
PUT /_template/template_1
{
"index_patterns": ["zx*", "bar*"],
"settings": {
"index": {
"routing": {
"allocation": {
"require": {
"box_type": "warm"
}
}
},
"refresh_interval": "2s",
"number_of_shards": "5",
"translog": {
"sync_interval": "60s",
"durability": "async"
},
"number_of_replicas": "1"
}
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"path_match": "@message",
"mapping": {
"norms": false,
"type": "text"
},
"match_mapping_type": "string"
}
},
{
"string_fields": {
"mapping": {
"type": "keyword",
"fields": {
"smart": {
"norms": false,
"type": "text"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"properties": {
"@timestamp": {
"type": "date"
}
}
}
}
}
说明:
- 匹配任意字段名,如果该字段是string,那么将该字段映射为keyword, 同时创建一个fieldname.smart字段,该字段是不分词的,映射为text
- 当遇到@message这个字段的时候,会将该字段映射为text,并且设置为不分词
- 写入es的每个doc的数据都会带上@timestamp这个字段,是date类型
4.
match_mapping_type
字段表示允许只对特定类型的字段使用模板,这里使用的是string
参考文档:
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices-templates.html#delete