Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: slightly mis-stated logic.

Table of Contents

Pure Negative Queries

One common situation encountered in search with Lucene-based programs is the problem of pure negative query clauses. This is a query clause that consists ONLY of negative phrases, using the - notation or the "NOT" boolean operator.

...

This is because when the entire query is a single negative clause without any complexity such as parentheses, Solr is able to detect the impossible situation and fix it for you. A query of -field:value is translated internally into *:* -field:value by Solr before it gets sent down to the Lucene layer – a starting point of all documents is implicitly added to the query, and then the subtraction works.

...

The fact that this works but similar queries using "OR" don't work is NOT a bug. It's just a byproduct of the way that boolean and MUST/SHOULD/MUST NOT query logic works. The query with "AND" works because "field1:value1" is the starting point for the query, then documents where field2 contains value2 are subtracted from that starting pointWith this structure, Solr is able to detect and fix the problem with the negative query in the second clause, but with the OR, it can't.