由于您要匹配包含f04但不包含z的任何字符串,因此您实际上可以使用[^z]*fo4[^z]* 详细信息 [^z]*-除z 之外的任何0+个字符 fo4-fo4子字符串 [^z]*-除z之外的任何0+个字符.如果您有一个要排除的多字符字符串(例如,z4而不是z),则可以使用补数运算符:.*f04.*&~(.*z4.*)这意味着几乎相同,但不支持换行符: .*-除换行符外的其他任何字符,请尽可能多 f04-f04 .*-除换行符外的其他任何字符,请尽可能多 &-AND ~(.*z4.*)-除具有z4 的字符串以外的任何字符串The mapping of my Elastic search looks like below:{ "settings": { "index": { "number_of_shards": "5", "number_of_replicas": "1" } }, "mappings": { "node": { "properties": { "field1": { "type": "keyword" }, "field2": { "type": "keyword" }, "query": { "properties": { "regexp": { "properties": { "field1": { "type": "keyword" }, "field2": { "type": "keyword" } } } } } } } }}Problem is :I am forming ES queries using elasticsearch_dsl Q(). It works perfectly fine in most of the cases when my query contains any complex regexp. But it totally fails if it contains regexp character '!' in it. It doesn't give any result when the search term contains '!' in it.For eg:1.) Q('regexp', field1 = "^[a-z]{3}.b.*") (works perfectly)2.) Q('regexp', field1 = "^f04.*") (works perfectly)3.)Q('regexp', field1 = "f00.*") (works perfectly)4.) Q('regexp', field1 = "f04baz?") (works perfectly)Fails in below case:5.) Q('regexp', field1 = "f04((?!z).)*") (Fails with no results at all)I tried adding "analyzer":"keyword" along with "type":"keyword" as above in the fields, but in that case nothing works.In the browser i tried to check how analyzer:keyword will work on the input on the case it fails:http://localhost:9210/search/_analyze?analyzer=keyword&text=f04((?!z).)*Seems to look fine here with result:{ "tokens": [ { "token": "f04((?!z).)*", "start_offset": 0, "end_offset": 12, "type": "word", "position": 0 } ]}I'm running my queries like below:search_obj = Search(using = _conn, index = _index, doc_type = _type).query(Q('regexp', field1 = "f04baz?"))count = search_obj.count()response = search_obj[0:count].execute()logger.debug("total nodes(hits):" + " " + str(response.hits.total))PLease help, its really a annoying problem as all the regex characters work fine in all the queries except !.Also, how do i check what analyzer is currently applied with above setting in my mappings? 解决方案 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 zfo4 - 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 possiblef04 - f04.* - any chars other than newline, as many as possible& - AND~(.*z4.*) - any string other than the one having z4 这篇关于负前瞻Regexp在ES DSL查询中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-26 05:13