Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Solr/Lucene behaviors with boolean operators

Tags:

solr

lucene

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.

like image 967
Nicolò Martini Avatar asked Sep 09 '11 15:09

Nicolò Martini


1 Answers

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.

like image 50
Nicolò Martini Avatar answered Sep 25 '22 14:09

Nicolò Martini