索引可使用预定义的模板进行创建,这个模板称作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将合成一个配置应用在这个索引上。合并的顺序可由索引模板的order属性来控制。
curl -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的样例如下:
{
"template-logstash" : {
"template" : "logstash*",
"settings" : {
"index.number_of_shards" : 5,
"number_of_replicas" : 1,
"index" : {
"store" : {
"compress" : {
"stored" : true,
"tv": true
}
}
}
},
"mappings" : {
"_default_" : {
"properties" : {
"dynamic" : "true",
},
},
"loadbalancer" : {
"_source" : {
"compress" : true,
},
"_ttl" : {
"enabled" : true,
"default" : "10d"
},
"_all" : {
"enabled" : false
},
"properties" : {
"@fields" : {
"dynamic" : "true",
"properties" : {
"client" : {
"type" : "string",
"index" : "not_analyzed"
},
"domain" : {
"type" : "string",
"index" : "not_analyzed"
},
"oh" : {
"type" : "string",
"index" : "not_analyzed"
},
"responsetime" : {
"type" : "double",
},
"size" : {
"type" : "long",
"index" : "not_analyzed"
},
"status" : {
"type" : "string",
"index" : "not_analyzed"
},
"upstreamtime" : {
"type" : "double",
},
"url" : {
"type" : "string",
"index" : "not_analyzed"
}
}
},
"@source" : {
"type" : "string",
"index" : "not_analyzed"
},
"@timestamp" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"@type" : {
"type" : "string",
"index" : "not_analyzed",
"store" : "no"
}
}
}
}
}
}
【参考】:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html