- Index Template 索引模板,顾名思义,就是创建索引的模具,其中可以定义一系列规则来帮助我们构建符合特定业务需求的索引的 mappings 和 settings,通过使用 Index Template 可以让我们的索引具备可预知的一致性。
常见的场景: 分割索引
分割索引就是根据时间间隔把一个业务索引切分成多个索引。
比如 把order_info 变成 order_info_20200101,order_info_20200102 ……
这样做的好处有两个:
- 1.结构变化的灵活性:因为elasticsearch不允许对数据结构进行修改。但是实际使用中索引的结构和配置难免变化,那么只要对下一个间隔的索引进行修改,原来的索引位置原状。这样就有了一定的灵活性。
- 2.查询范围优化: 因为一般情况并不会查询全部时间周期的数据,那么通过切分索引,物理上减少了扫描数据的范围,也是对性能的优化。
创建模板
PUT _template/movie_template
{
"index_patterns": ["movie_info*"],
"settings": {
"number_of_shards": 3
},
"aliases" : {
"{index}-query": {},
"movie_info-query":{}
},
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "keyword"
},
"movie_name": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
测试
POST movie_info_20200616/_doc
{
"id":"0101",
"movie_name":"白夜追凶2"
}
POST movie_info_20200617/_doc
{
"id":"0102",
"movie_name":"中国人"
}
进行查询
GET /movie_info_20200616/_search
GET /movie_info_20200617/_search
或者使用
GET movie_info-query/_search
进行分组查询(一般来说是dt字段分组,但我没有dt,所以用的电影名字)
GET movie_info-query/_search
{
"query": {
"term": {
"movie_name": {
"value": "中国人"
}
}
}
}
查看系统中已有的模板清单
GET _cat/templates