多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Rollover Index/滚动索引 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html) 译文链接 : [Rollover Index/滚动索引](/pages/viewpage.action?pageId=4882801) 贡献者 : [Le-Mon](/display/~tanwen) 当现有索引被认为太大或太旧时,滚动索引API会将别名滚动到新的索引。 API接受单个别名和条件列表。 别名只能指向一个索引。 如果索引满足指定的条件,则创建一个新的索引,并将别名切换到指向新的索引。 ``` curl -XPUT 'localhost:9200/logs-000001 ?pretty' -d' { "aliases": { "logs_write": {} } }' # Add > 1000 documents to logs-000001 curl -XPOST 'localhost:9200/logs_write/_rollover ?pretty' -d' { "conditions": { "max_age": "7d", "max_docs": 1000 } }' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO122-1) | 创建索引 `logs-0000001` 别名为 `logs_write`. | | [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO122-2) | 如果 ogs_write 指向的索引是在7天以前创建的,或者包含1000个以上的文档,则会创建 logs-000002索引,并更新logs_write别名以指向logs-000002. | 上述可能会返回如下的响应: ``` { "acknowledged": true, "shards_acknowledged": true, "old_index": "logs-000001", "new_index": "logs-000002", "rolled_over": true, "dry_run": false, "conditions": { "[max_age: 7d]": false, "[max_docs: 1000]": true } } ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO123-1) | index 是否被滚动. | | [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO123-2) | Whether the rollover was dry run. | | [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO123-3) | 条件的列表. | ## Naming the new index/新索引名称 如果现有索引的名称以 - 和数字结尾。 logs-000001 - 然后新索引的名称将遵循相同的模式,增加数字(logs-000002)。 无论旧索引名称如何,编号为零填充长度为6。 如果旧名称与此模式不匹配,则必须按照如下所示,指定新索引的名称: ``` curl -XPOST 'localhost:9200/my_alias/_rollover/my_new_index_name?pretty' -d' { "conditions": { "max_age": "7d", "max_docs": 1000 } }' ``` ## Using date math with the rolllover API/使用滚动API的日期计算 使用[日期计算](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html "Date math support in index names"): 根据索引滚动的日期来命名滚动索引是有用的技术,例如  `logstash-2016.02.03`.。 滚动API支持日期,但要求索引名称以一个破折号后跟一个数字,例如 logstash-2016.02.03-1,每次索引滚动时都会增加。 例如 ``` # PUT /<logs-{now/d}-1> with URI encoding: curl -XPUT 'localhost:9200/%3Clogs-%7Bnow%2Fd%7D-1%3E ?pretty' -d' { "aliases": { "logs_write": {} } }' curl -XPUT 'localhost:9200/logs_write/log/1?pretty' -d' { "message": "a dummy log" }' # Wait for a day to pass curl -XPOST 'localhost:9200/logs_write/_rollover ?pretty' -d' { "conditions": { "max_docs": "1" } }' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO124-1) | `创建当日的索引 logs-2016.10.31-1` | | [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html#CO124-2) | 当日索引滚动, 立即生成如. `logs-2016.10.31-000002` , 或者 `logs-2016.11.01-000002` 24小时后 | 然后可以按照 [日期数学文档](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html "Date math support in index names") 中的描述来引用这些索引。 例如,要搜索过去三天创建的索引,您可以执行以下操作: ``` # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search 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/定义新索引  新索引的设置,映射和别名取自任何匹配的[索引模板](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html "Index Templates")。 此外,您可以在请求正文中指定设置,映射和别名,就像 [create index](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html "Create Index")  API一样。 请求中指定的值覆盖匹配索引模板中设置的任何值。 例如,以下滚动请求将覆盖index.number_of_shardssetting: ``` curl -XPUT 'localhost:9200/logs-000001?pretty' -d' { "aliases": { "logs_write": {} } }' curl -XPOST 'localhost:9200/logs_write/_rollover?pretty' -d' { "conditions" : { "max_age": "7d", "max_docs": 1000 }, "settings": { "index.number_of_shards": 2 } }' ``` ## Dry run/干运行 滚动API支持dry_run模式,可以在不执行实际滚动的情况下检查请求条件: ``` curl -XPUT 'localhost:9200/logs-000001?pretty' -d' { "aliases": { "logs_write": {} } }' curl -XPOST 'localhost:9200/logs_write/_rollover?dry_run&pretty' -d' { "conditions" : { "max_age": "7d", "max_docs": 1000 } }' ``` ## Wait For Active Shards/等待激活分片 因为滚动操作会创建一个新的索引,因此在创建索引时的wait_for_active_shards 设置也适用于滚动操作。