You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用ILM清理Elasticsearch中过期且未关联别名的索引?

如何用ILM定期清理Elasticsearch中过期且无别名的索引

ILM原生的delete阶段没有直接内置“仅删除无别名过期索引”的配置,但可以通过以下几种方式实现需求:

方法1:ILM+索引模板(基于命名规则)

如果你的无别名索引有统一的命名前缀/后缀,可通过索引模板给这类索引绑定带删除规则的ILM策略:

步骤1:创建ILM删除策略

PUT _ilm/policy/delete_unaliased_expired
{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "Xd", // 替换为你的过期天数,比如30d
        "actions": {
          "delete": {}
        }
      }
    }
  }
}

步骤2:创建匹配无别名索引的模板

假设无别名索引都以unaliased-开头:

PUT _index_template/unaliased_indices_template
{
  "index_patterns": ["unaliased-*"],
  "template": {
    "settings": {
      "index.lifecycle.name": "delete_unaliased_expired"
    }
  }
}

后续新创建的符合命名规则的索引会自动应用该ILM策略,到期后被删除。

方法2:ILM+Watcher(无命名规则限制)

如果无法通过命名区分索引,可使用Watcher定期识别符合条件的索引,直接触发删除:

创建清理Watcher

PUT _watcher/watch/cleanup_unaliased_expired
{
  "trigger": {
    "schedule": {
      "daily": {"at": "02:00"} // 每天凌晨2点执行
    }
  },
  "input": {
    "http": {
      "request": {
        "method": "GET",
        "url": "_cat/indices?v&h=index,creation.date.string,aliases&s=creation.date"
      }
    }
  },
  "condition": {
    "script": {
      "source": """
        def expiredThreshold = System.currentTimeMillis() - (X * 24 * 60 * 60 * 1000);
        def lines = ctx.payload.split('\n').drop(1);
        return lines.stream().anyMatch(line -> {
          def parts = line.split(/\s+/);
          def createTime = ZonedDateTime.parse(parts[1]).toInstant().toEpochMilli();
          def hasAlias = !parts[2].equals('-');
          return createTime < expiredThreshold && !hasAlias;
        });
      """
    }
  },
  "actions": {
    "delete_matching_indices": {
      "indices": {
        "delete": {
          "indices": "{{#ctx.payload.split('\n').drop(1)}}{{#line.split(/\\s+/)}}{{^parts[2] != '-'}}{{^ZonedDateTime.parse(parts[1]).toInstant().toEpochMilli() > (System.currentTimeMillis() - (X * 24 * 60 * 60 * 1000))}}{{parts[0]}},{{/}}{{/}}{{/}}"
        }
      }
    }
  }
}

将X替换为你的过期天数,Watcher会自动筛选并删除符合条件的索引。

方法3:Curator替代方案

如果ILM的组合方案过于复杂,Curator工具更适合这类自定义索引清理场景:

Curator配置示例(action.yml)

actions:
  1:
    action: delete_indices
    description: 删除创建超过X天且无别名的索引
    options:
      ignore_empty_list: True
    filters:
      - filtertype: age
        source: creation_date
        direction: older
        unit: days
        unit_count: X
      - filtertype: alias
        exclude: True
        aliases: []

运行Curator时加载该配置即可完成定期清理。


内容的提问来源于stack exchange,提问作者Shobhit Tyagi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:55:22