索引可使用预定义的模板进行创建,这个模板称作Index templates。模板设置包括settings和mappings,通过模式匹配的方式使得多个索引重用一个模板,例如:
定义模板:
curl -XPUT localhost:9200/_template/template_1 -d
{
"template" : "te*",
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"_source" : {
"enabled" : false }
}
}
}
上述定义的模板template_1将对用te开头的新索引都是有效。
模板中也可以包含别别名的定义,如下:
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" : {
}
}
}
删除模板:
使用模板名称对模板进行删除.
curl -XDELETE localhost:9200/_template/template_1
查看定义的模板:
curl -XGET localhost:9200/_template/template_1
多个索引模板:
当存在多个索引模板时并且某个索引两者都匹配时,settings和mpapings将合