原文地址:https://gitbook.cn/books/5c52c6923417565017a61ce0/index.html


结合实践经验,大索引设计建议:使用模板+Rollover+Curator动态创建索引。
动态索引使用效果如下:

  1. index_2019-01-01-000001
  2. index_2019-01-02-000002
  3. index_2019-01-03-000003
  4. index_2019-01-04-000004
  5. index_2019-01-05-000005

使用模板统一配置索引

目的:统一管理索引,相关索引字段完全一致。

使用 Rollver 增量管理索引

目的:按照日期、文档数、文档存储大小三个维度进行更新索引。
使用举例:

  1. POST /logs_write/_rollover
  2. {
  3. "conditions": {
  4. "max_age": "7d",
  5. "max_docs": 1000,
  6. "max_size": "5gb"
  7. }
  8. }

**

索引增量更新原理

ES大索引设计建议 - 图1
索引更新的时机是:当原始索引满足设置条件的三个中的一个的时候,就会更新为新的索引。为保证业务的全索引检索,一般采用别名机制。

在索引模板设计阶段,模板定义一个全局别名:用途是全局检索,如图所示的别名:indexall。 _每次更新到新的索引后,新索引指向一个用于实时新数据写入的别名,如图所示的别名:index_latest。 同时将旧索引的别名 index_latest 移除。

别名删除新增操作举例:

  1. POST /_aliases
  2. {
  3. "actions" : [
  4. { "remove" : { "index" : "index_2019-01-01-000001", "alias" : "index_latest" } },
  5. { "add" : { "index" : "index_2019-01-02-000002", "alias" : "index_latest" } }
  6. ]
  7. }

使用 curator 高效清理历史数据

目的:按照日期定期删除、归档历史数据。

一个大索引的数据删除方式只能使用 delete_by_query,由于 ES 中使用更新版本机制。删除索引后,由于没有物理删除,磁盘存储信息会不减反增。有同学就反馈 500GB+ 的索引 delete_by_query 导致负载增高的情况。
而按照日期划分索引后,不需要的历史数据可以做如下的处理。

  • 删除。对应 delete 索引操作。
  • 压缩。对应 shrink 操作。
  • 段合并。对应 force_merge 操作。

而这一切,可以借助:curator 工具通过简单的配置文件结合定义任务 crontab 一键实现。
举例,一键删除 30 天前的历史数据:

  1. [root@localhost .curator]# cat action.yml
  2. actions:
  3. 1:
  4. action: delete_indices
  5. description: >-
  6. Delete indices older than 30 days (based on index name), for logstash-
  7. prefixed indices. Ignore the error if the filter does not result in an
  8. actionable list of indices (ignore_empty_list) and exit cleanly.
  9. options:
  10. ignore_empty_list: True
  11. disable_action: False
  12. filters:
  13. - filtertype: pattern
  14. kind: prefix
  15. value: logs_
  16. - filtertype: age
  17. source: name
  18. direction: older
  19. timestring: '%Y.%m.%d'
  20. unit: days
  21. unit_count: 30