Weird Solr/Lucene behaviors with boolean operators
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 expected results (lot), but if I put some parenthesis like
-text AND (-text)
or
(-text) AND (-text)
then I get no results at all. I can't understand why. Do you have an explanation for this behavior?
Thank you in advance.
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.
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?!
精彩评论