Weird Solr/Lucene behaviors with boolean operators

前端 未结 2 417
北荒
北荒 2020-12-03 17:47

I\'m crashing into a weird behavior with - operators in Solr/Lucene query syntax. If I execute the query

-text AND -text 

I get all expec

相关标签:
2条回答
  • 2020-12-03 18:33

    The question have been answered very well in Solr mailing list. They have also added an entry in the offical FAQ, that says:

    Boolean queries must have at least one "positive" expression (ie; MUST or SHOULD) in order to match. Solr tries to help with this, and if asked to execute a BooleanQuery that does contains only negatived clauses at the topmost level, it adds a match all docs query (ie: *:*)

    If the top level BoolenQuery contains somewhere inside of it a nested BooleanQuery which contains only negated clauses, that nested query will not be modified, and it (by definition) an't match any documents -- if it is required, that means the outer query will not match.

    So expressions with only "negative" values return always 0 results, except at the topmost level, where the parser silently add a *:* at the beginning of the query.

    Therefore -text AND -text is transformed to *:* -text AND -text and so it has results, while (-text) isn't transformed to (*:* -text), because it is not at the topmost level, and so (-text) gives no results.

    0 讨论(0)
  • 2020-12-03 18:50

    The explanation depends on the search-handler you are using and the whole query string.

    For example: you can search on multiple fields, like

    text AND text <- which searched in all (default) fields - depending on the search handler

    FIELD1:text AND text <- which uses AND to search inside FIELD1

    FIELD1:text1 AND (-text2) <- searches (as i remember right) for text1 in FIELD1 AND NOT text2 in all other (default) documents - depending on the search handler.

    I think, using () changes the scope / fields of the search.

    Maybe the behaviour you describe hast to do with something like that?!

    0 讨论(0)
提交回复
热议问题