原文地址:https://gitbook.cn/books/5c52c6923417565017a61ce0/index.html
结合实践经验,大索引设计建议:使用模板+Rollover+Curator动态创建索引。
动态索引使用效果如下:
index_2019-01-01-000001index_2019-01-02-000002index_2019-01-03-000003index_2019-01-04-000004index_2019-01-05-000005
使用模板统一配置索引
目的:统一管理索引,相关索引字段完全一致。
使用 Rollver 增量管理索引
目的:按照日期、文档数、文档存储大小三个维度进行更新索引。
使用举例:
POST /logs_write/_rollover{"conditions": {"max_age": "7d","max_docs": 1000,"max_size": "5gb"}}
**
索引增量更新原理

索引更新的时机是:当原始索引满足设置条件的三个中的一个的时候,就会更新为新的索引。为保证业务的全索引检索,一般采用别名机制。
在索引模板设计阶段,模板定义一个全局别名:用途是全局检索,如图所示的别名:indexall。 _每次更新到新的索引后,新索引指向一个用于实时新数据写入的别名,如图所示的别名:index_latest。 同时将旧索引的别名 index_latest 移除。
别名删除和新增操作举例:
POST /_aliases{"actions" : [{ "remove" : { "index" : "index_2019-01-01-000001", "alias" : "index_latest" } },{ "add" : { "index" : "index_2019-01-02-000002", "alias" : "index_latest" } }]}
使用 curator 高效清理历史数据
目的:按照日期定期删除、归档历史数据。
一个大索引的数据删除方式只能使用 delete_by_query,由于 ES 中使用更新版本机制。删除索引后,由于没有物理删除,磁盘存储信息会不减反增。有同学就反馈 500GB+ 的索引 delete_by_query 导致负载增高的情况。
而按照日期划分索引后,不需要的历史数据可以做如下的处理。
- 删除。对应 delete 索引操作。
- 压缩。对应 shrink 操作。
- 段合并。对应 force_merge 操作。
而这一切,可以借助:curator 工具通过简单的配置文件结合定义任务 crontab 一键实现。
举例,一键删除 30 天前的历史数据:
[root@localhost .curator]# cat action.ymlactions:1:action: delete_indicesdescription: >-Delete indices older than 30 days (based on index name), for logstash-prefixed indices. Ignore the error if the filter does not result in anactionable list of indices (ignore_empty_list) and exit cleanly.options:ignore_empty_list: Truedisable_action: Falsefilters:- filtertype: patternkind: prefixvalue: logs_- filtertype: agesource: namedirection: oldertimestring: '%Y.%m.%d'unit: daysunit_count: 30
