solrsolr-query-syntax

Solr OR query does not return any result while queries returns result separately


I am trying to run OR query on Solr 6.6 like "field:[x TO ] OR -field:[ TO *]" which means greater than or equal some value or null. Even both field:[x TO ] and -field:[ TO *] has result sets individually this OR query does return empty set. When I use parenthesis like "(field:[x TO ]) OR (-field:[ TO *])" it returns the result of field:[x TO *] only. Can you explain me what the problem with this query here?


Solution

  • This is caused by the second part of your second statement (OR -field:[x TO *]). Think of the - operator as subtracting the second set from the first one - but the problem here is that you don't have anything to subtract the second set from.

    When the -field:foo query is the only part of the query, Solr starts with the complete set of all documents, so that in a sense the query *:* (all documents) is prefixed to your query. When your query is later in a set of clauses, this isn't longer the case - it's only prefixed to the first clause (if at all).

    To solve this, be explicit: OR (*:* -field:[x TO *]).