建了 ISM policy,索引卻遲遲沒按策略執行動作。根因通常是三類:策略沒綁定到索引、轉移條件沒滿足(比如
min_index_age: 30d還沒到 30 天)、或還沒到job_interval的檢查週期(ISM 不是實時執行,是週期掃描)。
Issue & Env(案發現場與環境)
- 業務場景:建了
logs_retention策略想 30 天后刪索引,綁了策略後等了一陣,索引還在。 - 環境:OpenSearch 3.x(Docker latest),單節點。
- 關鍵區分:ISM 是「週期檢查」不是「實時觸發」;條件要滿足、綁定要生效、job_interval 要到點。
StackTrace(返回體現狀)
查索引是否綁了策略、處於什麼狀態:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/_plugins/_ism/explain/logs-2026-01?pretty"
返回 explain 裡沒有 policy 信息,或狀態一直停在 hot 沒轉移。
Reproduce(復現程式碼)
# ① 建策略:30 天后删
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/_plugins/_ism/policies/logs_retention?pretty" \
-H "Content-Type: application/json" \
-d '{ "policy": { "description": "retention", "default_state": "hot", "states": [
{ "name": "hot", "actions": [], "transitions": [ { "state_name": "delete", "conditions": { "min_index_age": "30d" } } ] },
{ "name": "delete", "actions": [ { "delete": {} } ], "transitions": [] }
] } }'
# ② 忘了绑定(或绑到了别的索引),索引一直不删
curl -sk -u admin:Devfix@2026 "https://localhost:9200/_plugins/_ism/explain/logs-2026-01?pretty"
判定標準:explain 裡沒有 policy_id(沒綁定),或綁了但 min_index_age: 30d 沒到、狀態停在 hot。
Root Cause(根因分析)
策略要顯式綁定到索引
建策略不會自動作用到索引。要麼用 ism_template(index_patterns + priority)讓策略自動應用到新建索引,要麼手動 POST _plugins/_ism/add/<index> 綁定。沒綁定,策略就是個「沒人用的模板」。
轉移條件要滿足
min_index_age: 30d 表示索引要「活了 30 天」才轉移。剛建的索引 30 天沒到,條件為 false,ISM 不會動它。時間條件(min_index_age/min_state_age)都是按索引創建時間/進入狀態時間算的。
ISM 按 job_interval 週期檢查,不是實時
ISM 按 job_interval(預設通常 5 分鐘)週期掃描,不是條件一滿足就立刻執行。剛滿足條件可能還要等一個週期才輪到它。
The Fix(解決方案)
第一步:確認綁定
curl -sk -u admin:Devfix@2026 "https://localhost:9200/_plugins/_ism/explain/logs-2026-01?pretty"
看 explain 裡有沒有 policy_id。沒有就綁:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_plugins/_ism/add/logs-2026-01?pretty" \
-H "Content-Type: application/json" \
-d '{ "policy_id": "logs_retention" }'
第二步:用 ism_template 讓策略自動套新建索引
{
"policy": {
"ism_template": { "index_patterns": [ "logs-*" ], "priority": 100 },
"states": [ ... ]
}
}
這樣以後 logs-* 新建的索引自動綁上策略。
第三步:短時間驗證用短條件
本地驗證別等 30 天,用 min_state_age: 1m 之類短時間,或臨時調小 job_interval。
注意事項
- 完整用法見 從零跑通 OpenSearch 索引模板與生命週期。
References(參考來源)
- ISM policies — OpenSearch 官方文檔opensearch-docs
