Searching for date range or null/no field in Solr [duplicate]
Asked Answered
M

2

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 *])
Metralgia answered 27/8, 2009 at 21:20 Comment(4)
Try this.Sheldonshelduck
Perfect, thanks. For clarity, exact solution is: -(-myfield:[start TO finish] AND myfield:[* TO *])Metralgia
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.Davilman
@Davilman : agreed, Solr should detect this and internally rewrite the expression to something it can handle.Sheldonshelduck
S
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.

Simonsen answered 26/5, 2014 at 14:1 Comment(0)
S
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.

Sweatt answered 20/2, 2013 at 10:33 Comment(1)
Hey @sebastien-lorber -- the query was checking MyDate was in the future or null right?Claudetta

© 2022 - 2024 — McMap. All rights reserved.