es模板使用

索引可使用预定义的模板进行创建,这个模板称作Index templates。

模板设置包括settings和mappings,通过模式匹配的方式使得多个索引重用一个模板,例如:
定义模板:
curl -XPUT localhost:9200/_template/template_1 -d '
{
"template" : "te*",
"settings" : {
"number_of_shards" : 5,
"number_of_replicas" : 1
},
"mappings" : {
"type1" : {
"_source" : {"enabled" : false }
}
}
} '
上述定义的模板 template_1 将对用te开头的新索引都是有效。
 
模板中也可以包含别别名的定义,如下:
curl -XPUT localhost:9200/_template/template_1 -d '
{
"template" : "te*",
"settings" : {
"number_of_shards" : 5,
"number_of_replicas" : 1
},
"aliases" : {
"{index}-alias-kimchy" : {
"filter" : {
"term" :{"user" : "kimchy" }
},
"routing" :"kimchy"
},
"{index}-alias" : {}
}
}

使用模板名称对模板进行删除.
curl -XDELETE localhost:9200/_template/template_1  
查看定义的模板
curl -XGET localhost:9200/_template 查看所有模板
curl -XGET localhost:9200/_template/template_1  

 
同时命中多个索引模板:
          当存在多个索引模板时并且某个索引两者都匹配时,settings和mpapings将合成一个配置应用在这个索引上。合并的顺序可由索引模板的order属性来控制。
url -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 }
}
}
} '  
上述order为1的配置将覆盖order为0的配置,最终索引的配置 source的enabled 为true。
 
模板配置文件:
         除了以上方式,索引模板也可以在文件中进行配置。索引模板的配置文件需要在每个
主节点的config目录下,目录结构为: config/templates/template_1.json, temp
late_1.json的样例如下:
{
"order": 0,
"template": "logstash*",
"settings": {
"index.number_of_shards": "2",
"index.store.compress.tv": "true",
"index.store.compress.stored": "true",
"index.number_of_replicas": "1"
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"omit_norms": true,
"type": "string"
},
"match_mapping_type": "string",
"match": "message"
}
},
{
"string_fields": {
"mapping": {
"index": "analyzed",
"omit_norms": false,
"type": "string",
"fields": {
"raw": {
"index": "not_analyzed",
"ignore_above": 256,
"type": "string",
"doc_values": true
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"_source": {
"compress": true
},
"_ttl": {
"enabled": true,
"default": "30d"
},
"properties": {
"geoip": {
"dynamic": "true",
"properties": {
"location": {
"type": "geo_point"
}
}
},
"@version": {
"index": "not_analyzed",
"type": "string",
"omit_norms": true
}
},
"_all": {
"enabled": false
}
}
},
"aliases": {}
}

你可能感兴趣的:(es)