negative lookahead Regexp doesnt work in ES dsl query

狂风中的少年 提交于 2019-12-01 21:22:24

ElasticSearch Lucene regex engine does not support any type of lookarounds. The ES regex documentation is rather ambiguous saying matching everything like .* is very slow as well as using lookaround regular expressions (which is not only ambiguous, but also wrong since lookarounds, when used wisely, may greatly speed up regex matching).

Since you want to match any string that contains f04 and does not contain z, you may actually use

[^z]*fo4[^z]*

Details

  • [^z]* - any 0+ chars other than z
  • fo4 - fo4 substring
  • [^z]* - any 0+ chars other than z.

In case you have a multicharacter string to "exclude" (say, z4 rather than z), you may use your approach using a complement operator:

.*f04.*&~(.*z4.*)

This means almost the same but does not support line breaks:

  • .* - any chars other than newline, as many as possible
  • f04 - f04
  • .* - any chars other than newline, as many as possible
  • & - AND
  • ~(.*z4.*) - any string other than the one having z4
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!