原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/conditionals-with-regex.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/conditionals-with-regex.html
本地英文版地址: ../en/conditionals-with-regex.html
重要: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 当前版本文档。
Conditionals with the Regular Expressionsedit
The if
conditional is implemented as a Painless script, which requires
explicit support for regular expressions.
script.painless.regex.enabled: true
must be set in elasticsearch.yml
to use regular
expressions in the if
condition.
If regular expressions are enabled, operators such as =~
can be used against a /pattern/
for conditions.
For example:
PUT _ingest/pipeline/check_url { "processors": [ { "set": { "if": "ctx.href?.url =~ /^http[^s]/", "field": "href.insecure", "value": true } } ] }
POST test/_doc/1?pipeline=check_url { "href": { "url": "http://www.elastic.co/" } }
Results in:
{ "_index": "test", "_type": "_doc", "_id": "1", "_version": 1, "_seq_no": 60, "_primary_term": 1, "found": true, "_source": { "href": { "insecure": true, "url": "http://www.elastic.co/" } } }
Regular expressions can be expensive and should be avoided if viable alternatives exist.
For example in this case startsWith
can be used to get the same result
without using a regular expression:
PUT _ingest/pipeline/check_url { "processors": [ { "set": { "if": "ctx.href?.url != null && ctx.href.url.startsWith('http://')", "field": "href.insecure", "value": true } } ] }