You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by lansing <ha...@wantedanalytics.com> on 2014/10/17 22:30:47 UTC

Strange behaviour with negatives queries

Hello,
We are using Solr cloud 4.8.1, I noticed this behaviour:
Searchig for    q=description:ruby AND (-title:java)    gives 0 results
But removing parenthesizes    q=description:ruby AND -title:java  gives
valid result
Pure negative query with parenthesizes also works well   q=(-title:java)
Is it a bug ?
Thank you



--
View this message in context: http://lucene.472066.n3.nabble.com/Strange-behaviour-with-negatives-queries-tp4164710.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Strange behaviour with negatives queries

Posted by lansing <ha...@wantedanalytics.com>.
Thank you for your reply



--
View this message in context: http://lucene.472066.n3.nabble.com/Strange-behaviour-with-negatives-queries-tp4164710p4165152.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Strange behaviour with negatives queries

Posted by Chris Hostetter <ho...@fucit.org>.
You're seeing an excentric edge case of the way "purely negative" "Boolean 
Queries" work.

in this example...

	description:ruby AND (-title:java)

you have a top level boolean query with two mandatory clauses. the second 
clause in that top level query is a (bested) boolean query with a single 
negative clause.

that nested boolean query with a single negative clause can never match 
anything, and since it's a mandatory clause in the top level boolean 
query, the entire query can never match anything.

In your second example...

	description:ruby AND -title:java

...you have a top level boolean query with two clauses -- the first is
mandatory and the second is prohibited (the "AND" is ignored for the 
second clause because the "-" overrides it -- see this link for reasons 
why i hate AND/OR and you shoulddefinitely not try to mix them with +/-: 
http://lucidworks.com/blog/why-not-and-or-and-not/ )

the end result is a top level query that will match anything matched by 
the first clause, excluding things matched by the second clause.

In your remaining examples is where a small bit of esoteric solr behavior 
comes into play...

	(-title:java)
	-title:java

...in both of these cases, you get a boolean query containing a single 
prohibitied clause.  in the first case the parents make that boolean query 
explicitly, in the second case the use of "-" to prohibit something force 
the parser to wrap it in a boolean query.

when solr sees that your top level query is a boolean query and *all* of 
the clauses of that top level boolean query are prohibited, solr tries to 
help "do what you mean" by re-writing that query to...

	(+*:* -title:java)

..ie: it adds a mandatory match all docs query as an additional clause.

this "do what you mean" logic applies only to the top level boolean query, 
it doesn't recurse into sub-queries -- both because it's kind of 
infeasible to do that in an nested structure of arbitrary queries (some of 
which might be custom plugins) but also because even in simple cases, it 
might not be what you want -- if you were programatically building up 
clauses of boolean queries from a form widget or something like that, your 
goal might in fact be that one particular nested-query only contain 
clauses $X & $Y.   if $X & $Y are both prohibited, then it might 
make sense to you that "($X $Y)" should match nothing, and thus "foo bar 
($X $Y)" should only match foo & bar, if it was suppose to match other 
stuff as well, the widget would have added some (positive) $Z to the 
nested query.



-Hoss
http://www.lucidworks.com/