如何在 Curator 过滤器上使用贪婪的正则表达式?
How to use greedy regexes on Curator filter?
我已将 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
.
如何修改过滤器以捕获两个索引?
Curator 的 Regex 引擎实现使用 U(Ungreedy)标志。
默认情况下,不贪婪的正则表达式使星号量词变得惰性,添加一个“?” Ungreedy 选项下的修饰符会将其转回 Greedy。
尝试添加“?”在正则表达式中的“.*”之后
'^xyz-us-(prod|preprod)-(.*?)-'
我在 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: {}
我已将 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
.
如何修改过滤器以捕获两个索引?
Curator 的 Regex 引擎实现使用 U(Ungreedy)标志。
默认情况下,不贪婪的正则表达式使星号量词变得惰性,添加一个“?” Ungreedy 选项下的修饰符会将其转回 Greedy。
尝试添加“?”在正则表达式中的“.*”之后
'^xyz-us-(prod|preprod)-(.*?)-'
我在 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: {}