Filter Solr for “Future Date or Not Set”

I’ve worked with some expression languages in my life. I have even created one for my masters thesis. Some of them are easy, some are complicated, some feel smooth, some are strange.

Recently Solr came to my life. Solr also supports some kind of expressions for queries and filters.

The Problem

I have entries of multiple types in my Solr index. There are event entries that have a date set, e.g:

    "name": "My Event",
    "startDate": "20181218T20:00:00Z",
    "endDate": "20181218T23:59:59Z",

And there are location entries without any date field, e.g:

 "name": "My Event Location",
 "address": "My Street 1",
 "city": "Enns",
 "zipcode": "4470",
 "country": "Austria"

Now what I want to achieve is a search by query term on all locations and all events that have not yet ended. So if I search for “My Event” I want to receive both entries if I search on December 18th. For the same query on December 19th I want to receive the location entry only.

The Solution

Just add a Filter Query (fq) to your query string, like so:

fq=(endDate:[NOW TO *] || false)

I really don’t know why it works. But it does. Strangely fq=(endDate:[NOW TO *] || false) works also 🙂

Of course I asked Google first, and there are some results, but none of them worked out for me. So If you can explain why ma filter works or even have a better solution, feel free to enlighten me in the comments below!

Author: Thomas Lemmé

I am a Java Developer currently working at one of the top APM vendors as a Java agent developer.

Leave a Reply

Your email address will not be published. Required fields are marked *