35

I want to perform a search on a text field in Solr. I want to return all matches in a range or where there is no value. The two searches word independently:

myfield:[start TO finish]
-myfield:[* TO *]

The first returns all matches in the range. The second returns all matches that have no value for the "myfield" field. The problem is combining these two.

This returns no matches:

myfield:[start TO finish] OR -myfield:[* TO *]

This returns matches between start and finish, but not null entries:

myfield:[start TO finish] OR (-myfield:[* TO *])
4
  • 1
    Try this. Aug 28, 2009 at 1:45
  • 1
    Perfect, thanks. For clarity, exact solution is: -(-myfield:[start TO finish] AND myfield:[* TO *]) Aug 28, 2009 at 3:10
  • Did anyone file this as a bug in the Solr JIRA? I don't see any reason why the positive-logic version shouldn't work.
    – samkass
    Sep 6, 2013 at 17:53
  • @samkass : agreed, Solr should detect this and internally rewrite the expression to something it can handle. Sep 6, 2013 at 18:55

2 Answers 2

11

The solution from Mauricio Scheffer worked for me until I've included it into full query. The query itself may contain up to three fields with ranges and somewhere in the middle Solr failed to process it. I have managed to solve it with next query:

(myfield:[start TO finish] OR (*:* NOT myfield:[* TO *]))

It woked even in my complex query, so maybe it will help someone else.

3

I agree with Mauricio Scheffer solution.

If that can help, I transformed my initial query:

DocSource:"P" OR ( DocSource:"E" AND (MyDate:[NOW TO *] OR -MyDate:[* TO *] ) )

To

DocSource:"P" OR ( DocSource:"E" AND -( -MyDate:[* TO NOW] AND MyDate:[* TO *] ) )

The first query didn't run as expected in Solr 4.1.

1
  • Hey @sebastien-lorber -- the query was checking MyDate was in the future or null right?
    – Jono
    Jun 16, 2020 at 21:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.