java Lucene 中的索引和搜索日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5495645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indexing and Searching Date in Lucene
提问by user660024
I tried it to index date with DateTools.dateToString()
method. Its working properly for indexing as well as searching.
我试过用DateTools.dateToString()
方法索引日期。它可以正常用于索引和搜索。
But my already indexed data which has some references is in such a way that it has indexed Date as a new Date().getTime()
.
但是我已经索引的数据有一些引用,它已将 Date 索引为新的Date().getTime()
.
So my problem is how to perform RangeSearch Query
on this data...
所以我的问题是如何RangeSearch Query
处理这些数据...
Any solution to this???
这个有什么解决办法???
Thanks in Advance.
提前致谢。
回答by WhiteFang34
You need to use a TermRangeQuery
on your date field. That field always needs to be indexed with DateTools.dateToString()
for it to work properly. Here's a full example of indexing and searching on a date range with Lucene 3.0:
您需要TermRangeQuery
在日期字段上使用 a 。该字段总是需要被索引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);
}
}
回答by Michael McCandless
You'll get much better search performance if you use a NumericField for your date, and then NumericRangeFilter/Query to do the range search.
如果您使用 NumericField 作为日期,然后使用 NumericRangeFilter/Query 进行范围搜索,您将获得更好的搜索性能。
You just have to encode your date as a long or int. One simple way is to call the .getTime() method of your Date, but this may be far more resolution (milli-seconds) than you need. If you only need down to the day, you can encode it as YYYYMMDD integer.
您只需将日期编码为 long 或 int。一种简单的方法是调用 Date 的 .getTime() 方法,但这可能比您需要的分辨率(毫秒)高得多。如果您只需要到当天,您可以将其编码为 YYYYMMDD 整数。
Then, at search time, do the same conversion on your start/end Dates and run NumericRangeQuery/Filter.
然后,在搜索时,对开始/结束日期进行相同的转换并运行 NumericRangeQuery/Filter。