1

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?

pokeRex110
  • 833
  • 2
  • 14
  • 26

1 Answers1

2

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.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • I already found a solution for Solr but I am not using it. The TermRangeQuery, as I suspected is the correct one. Some times ago I remember that Lucene introduced a optimization for dates. I think the first solution although I am not sure, does fit in the optimization. Worse case I could ask to the Lucene mailing list. Thank you – pokeRex110 Sep 23 '14 at 07:43