由于elasticsearch7.x不允许把索引级别的设置配置在elasticsearch.yml中,所以需要对每个索引进行单独的配置,这样的话就比较麻烦,可以索引的公共的配置放在索引模板中,故在新建索引的时候可以自动的设置到索引中。
常用的索引配置:
"number_of_replicas": 1, #索引副本数量,推荐副本数为1
"max_result_window": 100000,
#刷新间隔,当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到。
#这里对实时性要求不高,可以增加该值提高写入性能
"refresh_interval": "10s",
"number_of_shards":分片数量
"index.search.slowlog.threshold.query.warn": 10s,
"index.search.slowlog.threshold.query.info": 5s,
"index.search.slowlog.threshold.query.debug": 2s,
"index.search.slowlog.threshold.query.trace": 500ms,
"index.search.slowlog.threshold.fetch.warn": 1s,
"index.search.slowlog.threshold.fetch.info": 800ms,
"index.search.slowlog.threshold.fetch.debug": 500ms,
"index.search.slowlog.threshold.fetch.trace": 200ms,
"index.indexing.slowlog.threshold.index.warn": 10s,
"index.indexing.slowlog.threshold.index.info": 5s,
"index.indexing.slowlog.threshold.index.debug": 2s,
"index.indexing.slowlog.threshold.index.trace": 500ms
"dynamic": false #是否关闭动态字段映射,默认为true,这里选择个人选择禁用
1. 模板常用参数
- order字段:有时候一个模板可能满足绝大多数新建索引的需求,但是局部需要微调,此时,如果复制旧的模板,修改模板后,成为一个新的索引模板即可达到我们的需求,但是操作略显复杂。此时可以采用模板叠加与覆盖来操作。模板的优先级是order字段定义的,数字越大,优先级越高。如果存在多个模板,其order相同,那么久不能保证同order顺序。
- index_patterns:定义使用该模板索引的格式,例如test*,则使用test开头的索引均会使用该模板。
- settings:索引模板中的setting部分一般定义索引的分片信息、刷新时间、自定义分析器。
- mappings:字段映射。
2. 基本操作
# 删除索引模板
DELETE _template/test_template_default
# 查看test*开头的索引模板
GET _template/test*
3. 创建索引模板
创建索引模板:
PUT _template/test_template_default
{
"index_patterns":[
"template-*"
],
"settings":{
"number_of_replicas":1,
"refresh_interval":"10s",
"index.search.slowlog.threshold.query.warn": "10s",
"index.search.slowlog.threshold.query.info": "5s",
"index.search.slowlog.threshold.query.debug": "2s",
"index.search.slowlog.threshold.query.trace": "500ms"
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"@version": {
"index": "false",
"type": "text"
},
"logLevel": {
"type": "long"
}
}
},
"order":0
}
再次创建优先级高的索引模板:
# 创建模板类型===高优先级
PUT _template/test_template_demo
{
"index_patterns":[
"template-test*"
],
"settings":{
"number_of_replicas":1,
"number_of_shards":3,
"refresh_interval":"20s",
"index.search.slowlog.threshold.query.warn": "10s",
"index.search.slowlog.threshold.query.info": "5s",
"index.search.slowlog.threshold.query.debug": "2s",
"index.search.slowlog.threshold.query.trace": "500ms"
},
"mappings": {
"date_detection": false,
"properties": {
"@version": {
"type": "keyword"
},
"logLevel": {
"type": "long"
}
}
},
"order":1
}
创建索引:
PUT template-test-demo
查看索引的setting信息.png
查看索引的mapping信息.png
根据上图显示:优先级高的索引模板会覆盖优先级低的索引模板的配置信息,若优先级高的索引模板未配置信息,则使用优先级低的索引模板的配置信息。
4. 用途
例如收集日志,会导致索引越来越大,此时需要按照日期来切分索引,我们定义日志模板后,便可以规定一系列日志索引的格式。