You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by Apache Wiki <wi...@apache.org> on 2018/09/20 10:04:49 UTC

[Solr Wiki] Update of "NegativeQueryProblems" by ShawnHeisey

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Solr Wiki" for change notification.

The "NegativeQueryProblems" page has been changed by ShawnHeisey:
https://wiki.apache.org/solr/NegativeQueryProblems?action=diff&rev1=4&rev2=5

Comment:
changed query strings to monospace font.

  
  You might ask why a query like this works:
  
- /solr/corename/select?q=-field:value
+ `/solr/corename/select?q=-field:value`
  
  This is because when the entire query is a single negative clause, 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.
  
  These queries will not work as expected:
  
- '''-field1:value1 OR field2:value2'''
+ `-field1:value1 OR field2:value2`
  
- '''field2:value2 OR -field1:value1'''
+ `field2:value2 OR -field1:value1`
  
  When there are multiple clauses in the input query, Solr's negative query detection does not work, so the purely negative clauses in these queries cannot be automatically fixed.  If these queries are rewritten as the following, adding a starting point of "all documents" to the negative clause, then it works as expected:
  
- '''field2:value2 OR (*:* -field1=value1)'''
+ `field2:value2 OR (*:* -field1=value1)`
  
  Negative query clause problems can be very subtle.  This query looks similar to the ones above that don't work, but it DOES work just fine:
  
- '''field1:value1 AND -field2:value2'''
+ `field1:value1 AND -field2:value2`
  
  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 point.