Indexing and searching for dates in Lucenen
Declare
private date[] dates = {};
Storing dates in an array in a fixed format
private void Setdates () {
SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd");
dates = new Date[ids.length];
try {
Dates[0] = Sdf.parse ("2010-02-19");
DATES[1] = Sdf.parse ("2012-01-11");
DATES[2] = Sdf.parse ("2011-09-19");
DATES[3] = Sdf.parse ("2010-12-22");
DATES[4] = Sdf.parse ("2012-01-01");
} catch (ParseException e) {
e.printstacktrace ();
}
}
When indexing, it is not necessary to make the date segmentation and weighted values, and other operations
Doc.add (New Numericfield ("date", Field.store.yes,true). Setlongvalue (Dates[i].gettime ());
where SD is Scorcedoc
Search by ID, get ID corresponding to document
document DOC = Searcher.doc (sd.doc);
Converts a long type of date to date type
//from doc to get the value of the index in the date field to hold
Long num = long.valueof (doc.get ("date"));
SimpleDateFormat sdf1 = new SimpleDateFormat ("Yyyy-mm-dd");
Convert a long to date type
java.util.Date dt = new Date (num) by using the constructor function;
Formats the date type
String sdatetime = sdf1.format (DT);