Ron K
Ron K

Reputation: 109

Lucene Query on a DateField indexed by Solr

We are using a solr index for various search applications. In most cases we use it just as you would with the admin interface. for example:

+text:Mr +text:burns +publish_date[2012-09-10T00:00:00Z TO 2012-10-10T00:00:00Z]

This works fine.
My problem is that in one app we use complex lucene Queries directly against the index (without using solr) and in these queries i cant find how to search on a date field.
In schema.xml:

<field name="publish_date" type="date" indexed="true" stored="true"/>

It seems that solr stores the date as unix time in milliseconds, when pulling the field from the index it looks like that 1336867200000

In lucene I've tried every query I can think of:

TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "1299628800000", "1336867200000", true, true);
TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "1299628800", "1336867200", true, true);
TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "2012-09-10T00:00:00Z", "2012-10-10T00:00:00Z", true, true);
TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "20120910", "20121010", true, true);
NumericRangeQuery nq1 = NumericRangeQuery.newLongRange("publish_date", 1299628800, 1336867200, true, true);

and a few other tries...
The only query that did return results was

TermRangeQuery nq1 = new TermRangeQuery("publish_date",  null, null, true, true);

Of course this is a query with "Open endpoints" but it does seem to apply that the field is indexed as string (the same didn't work with int-range or long-range).
The frustrating thing is that it is obviously possible, I just have to figure out how dose solr preforms it's date range.
Anyone knows how to perform this search?
Any help would be appreciated.

Upvotes: 3

Views: 2187

Answers (1)

mindas
mindas

Reputation: 26733

The "1336867200000" is a timestamp, yes.

TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "1299628800000", "1336867200000", true, true);
TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "1299628800", "1336867200", true, true); 
TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "2012-09-10T00:00:00Z", "2012-10-10T00:00:00Z", true, true); 
TermRangeQuery nq1 = new TermRangeQuery("publish_date",  "20120910", "20121010", true, true);

Above ones won't work because you are using string parameter for numeric data.

NumericRangeQuery nq1 = NumericRangeQuery.newLongRange("publish_date", 1299628800, 1336867200, true, true);

This one wouldn't work because you didn't pad it with enough zeroes. 1299628800 stands for Fri Jan 16 02:00:28 GMT 1970.

If you have document(s) dated between 09/03/2011 and 13/05/2012, the following should work:

NumericRangeQuery nq1 = NumericRangeQuery.newLongRange("publish_date", 1299628800000L, 1336867200000L, true, true);

Upvotes: 2

Related Questions