4

我已设置 Curator 以通过此过滤器删除旧的 Elasticsearch 索引:

(...)
filters:
- filtertype: pattern
  kind: regex
  value: '^xyz-us-(prod|preprod)-(.*)-'
  exclude:
- filtertype: age
  source: name
  direction: older
  timestring: '%Y.%m.%d'
  unit: days
  unit_count: 7
  exclude:
(...)

但是,我意识到 Curator 使用非贪婪的正则表达式,因为此过滤器捕获索引xyz-us-prod-foo-2018.10.11但不捕获xyz-us-prod-foo-bar-2018.10.11.

如何修改过滤器以捕获两个索引?

4

2 回答 2

2

我在https://discuss.elastic.co/t/use-greedy-regexes-in-curator-filter/154200给出的答案仍然很好,尽管您不知何故无法得到我在那里发布的结果。锚定结尾并指定正则表达式为我工作的日期:'^xyz-us-(prod|preprod)-.*-\d{4}\.\d{2}\.\d{2}$'

我创建了这些索引:

PUT xyz-us-prod-foo-2018.10.11
PUT xyz-us-prod-foo-bar-2018.10.11
PUT xyz-us-preprod-foo-2018.10.12
PUT xyz-us-preprod-foo-bar-2018.10.12

并使用此配置运行:

---
actions:
  1:
    action: delete_indices
    filters:
    - filtertype: pattern
      kind: regex
      value: '^xyz-us-(prod|preprod)-.*-\d{4}\.\d{2}\.\d{2}$'
      exclude:
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 7

结果完全匹配:

2018-10-29 20:08:28,120 INFO               curator.utils           show_dry_run:928  DRY-RUN: delete_indices: xyz-us-preprod-foo-2018.10.12 with arguments: {}
2018-10-29 20:08:28,120 INFO               curator.utils           show_dry_run:928  DRY-RUN: delete_indices: xyz-us-preprod-foo-bar-2018.10.12 with arguments: {}
2018-10-29 20:08:28,120 INFO               curator.utils           show_dry_run:928  DRY-RUN: delete_indices: xyz-us-prod-foo-2018.10.11 with arguments: {}
2018-10-29 20:08:28,120 INFO               curator.utils           show_dry_run:928  DRY-RUN: delete_indices: xyz-us-prod-foo-bar-2018.10.11 with arguments: {}
于 2018-10-30T01:14:57.790 回答
1

Curator 对 Regex 引擎的实现使用了 U(Ungreedy)标志。

默认情况下,不贪婪的正则表达式使星量词变得懒惰,添加一个“?” Ungreedy 选项下的修饰符会将其变回 Greedy。

尝试添加一个“?” 在你的正则表达式中的 '.*' 之后

'^xyz-us-(prod|preprod)-(.*?)-'
于 2018-10-27T04:10:33.400 回答