pokeRex110
pokeRex110

Reputation: 855

Lucene 4.10 Date Range Query Api

I want to build a range query for a date field programmatically in Lucene 4.10, but I didn't find anyway to do that. My pseudocode would be:

new DateRangeQuery(dateLowerBound, dateUpperBound);

Is it a good idea to use org.apache.lucene.document.DateTool class to transform it and then use a NumericRangeQuery?

Upvotes: 1

Views: 1211

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

I'd pick one of two possibilities:

1 - Use DateTools to obtain a string representation good for indexing:

String indexableDateString = DateTools.dateToString(theDate, DateTools.Resolution.MINUTE);
doc.add(new StringField("importantDate", indexableDateString, Field.Store.YES));
...
TopDocs results = indexSearcher.search(new TermRangeQuery(
    "importantDate",
    new BytesRef(DateTools.dateToString(lowDate, DateTools.Resolution.MINUTE)),
    new BytesRef(DateTools.dateToString(highDate, DateTools.Resolution.MINUTE)),
    true,
    false
));
...
Field dateField = resultDocument.getField("importantDate")
Date retrievedDate = DateTools.stringToDate(dateField.stringValue());

2 - Skip date tools, and index the dates as numeric values using Date.getTime() or Calendar.getTimeInMillis(), or something similar:

long indexableDateValue = theDate.getTime();
doc.add(new LongField("importantDate", indexableDateValue, Field.Store.YES));
...
TopDocs results = indexSearcher.search(NumericRangeQuery.newLongRange(
    "importantDate",
    lowDate.getTime(),
    highDate.getTime(),
    true,
    false
));
...
Field dateField = resultDocument.getField("importantDate")
Date retrievedDate = new Date(dateField.numericValue());

I'd generally opt for the first, as it makes control over precision more obvious, but whichever strikes your fancy should work fine.

Also worth mentioning is solr's TrieDateField, though I wouldn't really recommend getting into that if you aren't already using solr anyway.

Upvotes: 2

Related Questions