建了 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
