आपको अपने दिनांक फ़ील्ड पर TermRangeQuery
का उपयोग करने की आवश्यकता है। ठीक से काम करने के लिए उस क्षेत्र को हमेशा DateTools.dateToString()
के साथ अनुक्रमित करने की आवश्यकता है। यहाँ अनुक्रमण की एक पूरी उदाहरण है और Lucene 3.0 के साथ किसी समय अवधि पर खोज:
public class LuceneDateRange {
public static void main(String[] args) throws Exception {
// setup Lucene to use an in-memory index
Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
MaxFieldLength mlf = MaxFieldLength.UNLIMITED;
IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);
// use the current time as the base of dates for this example
long baseTime = System.currentTimeMillis();
// index 10 documents with 1 second between dates
for (int i = 0; i < 10; i++) {
Document doc = new Document();
String id = String.valueOf(i);
String date = buildDate(baseTime + i * 1000);
doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("date", date, Store.YES, Index.NOT_ANALYZED));
writer.addDocument(doc);
}
writer.close();
// search for documents from 5 to 8 seconds after base, inclusive
IndexSearcher searcher = new IndexSearcher(directory);
String lowerDate = buildDate(baseTime + 5000);
String upperDate = buildDate(baseTime + 8000);
boolean includeLower = true;
boolean includeUpper = true;
TermRangeQuery query = new TermRangeQuery("date",
lowerDate, upperDate, includeLower, includeUpper);
// display search results
TopDocs topDocs = searcher.search(query, 10);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc);
}
}
public static String buildDate(long time) {
return DateTools.dateToString(new Date(time), Resolution.SECOND);
}
}
लुसीन का संस्करण, लुसीन <2.9 केवल लेक्सोग्राफिक रेंज क्वेरी करता है, आपको उस मामले में सटीक दिनांक प्रारूप निर्दिष्ट करने की आवश्यकता हो सकती है !! – Narayan
मैं 2.9.1 का उपयोग कर रहा हूं। क्या मुझे केवल विशिष्ट दिनांक प्रारूप का उपयोग करने की आवश्यकता है? यह GetTime() के साथ काम नहीं करेगा? – user660024