索引可使用预定义的模板进行创建,这个模板称作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"
}
}
}
}
}
}
索引模板
Elasticsearch 不要求在使用一个索引前创建它。 对于日志记录类应用,依赖于自动创建索引比手动创建要更加方便。Logstash 使用事件中的时间戳来生成索引名。 默认每天被索引至不同的索引中,因此一个 @timestamp 为 2014-10-01 00:00:01 的事件将被发送至索引 logstash-2014.10.01 中。 如果那个索引不存在,它将被自动创建。
通常我们想要控制一些新建索引的设置(settings)和映射(mappings)。也许我们想要限制分片数为 1 ,并且禁用 _all 域。 索引模板可以用于控制何种设置(settings)应当被应用于新创建的索引:
PUT /_template/my_logs -------------- [1]
{
"template": "logstash-*", -------------- [2]
"order": 1, -------------- [3]
"settings": {
"number_of_shards": 1 -------------- [4]
},
"mappings": {
"_default_": { -------------- [5]
"_all": {
"enabled": false
}
}
},
"aliases": {
"last_3_months": {} -------------- [6]
}
}
- 创建一个名为 my_logs 的模板
- 将这个模板应用于所有以 logstash- 为起始的索引
- 这个模板将会覆盖默认的 logstash 模板,因为默认模板的 order 更低
- 限制主分片数量为 1
- 为所有类型禁用 _all 域
- 添加这个索引至 last_3_months 别名中。
总结
这个模板指定了所有名字以 logstash- 为起始的索引的默认设置,不论它是手动还是自动创建的。 如果我们认为明天的索引需要比今天更大的容量,我们可以更新这个索引以使用更多的分片。
这个模板还将新建索引添加至了 last_3_months 别名中,然而从那个别名中删除旧的索引则需要手动执行。