滾動索引一般可以與索引模板結合使用,實現按一定條件自動創建索引。
1.當現有索引被認為太大或太舊時,滾動索引API會將別名滾動到新的索引。
PUT /logs-000001 { "aliases": { "logs_write": {} } } # Add > 1000 documents to logs-000001 POST /logs_write/_rollover { "conditions": { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" } }
創建索引 logs-0000001 別名為 logs_write.
如果 logs_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 } }
2.如果現有索引的名稱以 - 和數字結尾。 logs-000001 - 然后新索引的名稱將遵循相同的模式,增加數字(logs-000002)。 無論舊索引名稱如何,編號為零填充長度為6。
如果舊名稱與此模式不匹配,則必須按照如下所示,指定新索引的名稱:
POST /my_alias/_rollover/my_new_index_name { "conditions": { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" } }
3.使用日期計算: 根據索引滾動的日期來命名滾動索引是有用的技術,例如 logstash-2016.02.03
.。 滾動API支持日期,但要求索引名稱以一個破折號后跟一個數字,例如 logstash-2016.02.03-1,每次索引滾動時都會增加。 例如
# PUT /<logs-{now/d}-1> with URI encoding: PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E { "aliases": { "logs_write": {} } } PUT logs_write/_doc/1 { "message": "a dummy log" } POST logs_write/_refresh # Wait for a day to pass POST /logs_write/_rollover { "conditions": { "max_docs": "1" } }
4.索引名稱對日期計算的支持:
日期計算的格式
<static_name{date_math_expr{date_format|time_zone}}>
上述的說明 :
位置
|
說明
|
---|---|
static_name | 是名稱的 static text( 靜態文本)部分 |
date_math_expr | 是動態計算日期的動態 date math 表達式 |
date_format | 是計算日期應呈現的可選格式。默認是 YYYY.MM.dd |
time_zone | 是可選的時區。默認為 utc 。 |
必須將 date math 索引名稱表達式包含在尖括號中,並且所有的特殊字符都應進行 URI 編碼。例如 :
# GET /<logstash-{now/d}>/_search GET /%3Clogstash-%7Bnow%2Fd%7D%3E/_search { "query" : { "match": { "test": "data" } } }
用於 date 舍入的特殊字符必須按照如下 URI 編碼 :
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以下示例顯示了不同形式索引表達式和它們解析的 final index names(最終索引名稱),給定的當前時間是 2024 年 3 月 22 日 utc
Expression | Resolves to |
---|---|
|
|
|
|
|
|
|
|
|
|
如果索引中需要使用{}需要進行轉義處理
<elastic\\{ON\\}-{now/M}> resolves to elastic{ON}-2024.03.01
5.滾動API支持dry_run模式,可以在不執行實際滾動的情況下檢查請求條件:
PUT /logs-000001 { "aliases": { "logs_write": {} } } POST /logs_write/_rollover?dry_run { "conditions" : { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" } }