Rollover Index/滚动索引

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html

译文链接 : Rollover Index/滚动索引

贡献者 : Le-Mon

当现有索引被认为太大或太旧时,滚动索引API会将别名滚动到新的索引。 API接受单个别名和条件列表。 别名只能指向一个索引。 如果索引满足指定的条件,则创建一个新的索引,并将别名切换到指向新的索引。

  1. curl -XPUT 'localhost:9200/logs-000001 ?pretty' -d'
  2. {
  3. "aliases": {
  4. "logs_write": {}
  5. }
  6. }'
  7. # Add > 1000 documents to logs-000001
  8. curl -XPOST 'localhost:9200/logs_write/_rollover ?pretty' -d'
  9. {
  10. "conditions": {
  11. "max_age": "7d",
  12. "max_docs": 1000
  13. }
  14. }'

| Rollover Index/滚动索引 - 图1 | 创建索引 logs-0000001 别名为 logs_write. | | Rollover Index/滚动索引 - 图2 | 如果 ogs_write 指向的索引是在7天以前创建的,或者包含1000个以上的文档,则会创建 logs-000002索引,并更新logs_write别名以指向logs-000002. |

上述可能会返回如下的响应:

  1. {
  2. "acknowledged": true,
  3. "shards_acknowledged": true,
  4. "old_index": "logs-000001",
  5. "new_index": "logs-000002",
  6. "rolled_over": true,
  7. "dry_run": false,
  8. "conditions": {
  9. "[max_age: 7d]": false,
  10. "[max_docs: 1000]": true
  11. }
  12. }

| Rollover Index/滚动索引 - 图3 | index 是否被滚动. | | Rollover Index/滚动索引 - 图4 | Whether the rollover was dry run. | | Rollover Index/滚动索引 - 图5 | 条件的列表. |

Naming the new index/新索引名称

如果现有索引的名称以 - 和数字结尾。 logs-000001 - 然后新索引的名称将遵循相同的模式,增加数字(logs-000002)。 无论旧索引名称如何,编号为零填充长度为6。

如果旧名称与此模式不匹配,则必须按照如下所示,指定新索引的名称:

  1. curl -XPOST 'localhost:9200/my_alias/_rollover/my_new_index_name?pretty' -d'
  2. {
  3. "conditions": {
  4. "max_age": "7d",
  5. "max_docs": 1000
  6. }
  7. }'

Using date math with the rolllover API/使用滚动API的日期计算

使用日期计算: 根据索引滚动的日期来命名滚动索引是有用的技术,例如 logstash-2016.02.03.。 滚动API支持日期,但要求索引名称以一个破折号后跟一个数字,例如 logstash-2016.02.03-1,每次索引滚动时都会增加。 例如

  1. # PUT /<logs-{now/d}-1> with URI encoding:
  2. curl -XPUT 'localhost:9200/%3Clogs-%7Bnow%2Fd%7D-1%3E ?pretty' -d'
  3. {
  4. "aliases": {
  5. "logs_write": {}
  6. }
  7. }'
  8. curl -XPUT 'localhost:9200/logs_write/log/1?pretty' -d'
  9. {
  10. "message": "a dummy log"
  11. }'
  12. # Wait for a day to pass
  13. curl -XPOST 'localhost:9200/logs_write/_rollover ?pretty' -d'
  14. {
  15. "conditions": {
  16. "max_docs": "1"
  17. }
  18. }'

| Rollover Index/滚动索引 - 图6 | 创建当日的索引 logs-2016.10.31-1 | | Rollover Index/滚动索引 - 图7 | 当日索引滚动, 立即生成如. logs-2016.10.31-000002 , 或者 logs-2016.11.01-000002 24小时后 |

然后可以按照 日期数学文档 中的描述来引用这些索引。 例如,要搜索过去三天创建的索引,您可以执行以下操作:

  1. # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  2. curl -XGET 'localhost:9200/%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search?pretty'

Defining the new index/定义新索引

新索引的设置,映射和别名取自任何匹配的索引模板。 此外,您可以在请求正文中指定设置,映射和别名,就像 create index API一样。 请求中指定的值覆盖匹配索引模板中设置的任何值。 例如,以下滚动请求将覆盖index.number_of_shardssetting:

  1. curl -XPUT 'localhost:9200/logs-000001?pretty' -d'
  2. {
  3. "aliases": {
  4. "logs_write": {}
  5. }
  6. }'
  7. curl -XPOST 'localhost:9200/logs_write/_rollover?pretty' -d'
  8. {
  9. "conditions" : {
  10. "max_age": "7d",
  11. "max_docs": 1000
  12. },
  13. "settings": {
  14. "index.number_of_shards": 2
  15. }
  16. }'

Dry run/干运行

滚动API支持dry_run模式,可以在不执行实际滚动的情况下检查请求条件:

  1. curl -XPUT 'localhost:9200/logs-000001?pretty' -d'
  2. {
  3. "aliases": {
  4. "logs_write": {}
  5. }
  6. }'
  7. curl -XPOST 'localhost:9200/logs_write/_rollover?dry_run&pretty' -d'
  8. {
  9. "conditions" : {
  10. "max_age": "7d",
  11. "max_docs": 1000
  12. }
  13. }'

Wait For Active Shards/等待激活分片

因为滚动操作会创建一个新的索引,因此在创建索引时的wait_for_active_shards 设置也适用于滚动操作。