java - Lucene 4.10 Date Range Query Api -
i want build range query date field programmatically in lucene 4.10, didn't find anyway that. pseudocode be:
new daterangequery(datelowerbound, dateupperbound);
is idea use org.apache.lucene.document.datetool class transform , use numericrangequery?
i'd pick 1 of 2 possibilities:
1 - use datetools
obtain string representation 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, , index dates numeric values using date.gettime()
or calendar.gettimeinmillis()
, or 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 opt first, makes control on precision more obvious, whichever strikes fancy should work fine.
also worth mentioning solr's triedatefield
, though wouldn't recommend getting if aren't using solr anyway.
Comments
Post a Comment