Superior Solr Date Range Searching with RangeDateField

Searching by date range in Solr can be a pain. Let's look at a typical search where we want to see all blog posts for the month of February, 2016.

If you wanted precise date-time searching you can't beat the possibilities of standard Solr date range searching, but for 90% of the time it's overkill. Fortunately Solr provides a RangeDateField Type that makes date range searching a breeze. Let's look at the RangeDateField equivalent.

The Java

We're going to add a new rangedate String field to our PostDoc object and a Builder class constructor. Pretty minimal Java update requirements.

@Field(RANGE_DATE)
private String rangeDate;
// getter setter...

public Builder rangeDate(ZonedDateTime postDate) {
    built.rangeDate = postDate.toInstant().toString();
    return this;
}

The Solr

We only need to add a couple of lines to our Collection schema.xml file. First we add the solr.DateRangeField <fieldType />.

And our new rangedate field.

Updating the Site

For users of the NixMash Blog app, to get the benefits of RangeDate searching, after updating your app .JAR and the Solr schema.xml, do a quick restart of Solr Server and use the Reindex Posts Utility in NixMash Blog Administration.

Source Code Notes for this Post

Source code discussed in this post can be found in my NixMash Blog project located on GitHub.
Posted May 12, 2017 03:44 PM EDT

More Like This Post