【发布时间】:2017-06-03 12:22:13
【问题描述】:
我使用的是 Lucene 6.3,但我无法弄清楚以下非常基本的搜索查询有什么问题。它只是向每个文档添加一个日期范围,然后尝试在更大的范围内搜索应该找到两个文档。怎么了?
有内联 cmets 应该使示例非常自我解释。如果有任何不清楚的地方,请告诉我。
请注意,我的主要要求是能够与其他字段查询一起执行日期范围查询,例如
text:interesting date:[2014 TO NOW]
这是在看了Lucene spatial deep dive video的介绍之后,介绍了DateRangePrefixTree和策略所基于的框架。
咆哮:感觉如果我在这里犯了任何错误,我应该得到一些验证错误,无论是在查询上还是在写作上,考虑到我的例子是多么简单。
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.spatial.prefix.NumberRangePrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.PrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.DateRangePrefixTree;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public class TestLuceneDatePrefix {
/*
All these names should be lower case as field names are case sensitive in Lucene.
*/
private static final String NAME = "name";
public static final String TIME = "time";
private Directory directory;
private StandardAnalyzer analyzer;
private ScoreDoc lastDocOnPage;
private IndexWriterConfig indexWriterConfig;
@Before
public void setup() {
analyzer = new StandardAnalyzer();
directory = new RAMDirectory();
indexWriterConfig = new IndexWriterConfig(analyzer);
}
@Test
public void testAddDocumentAndSearchByDate() throws IOException {
IndexWriter w = new IndexWriter(directory, new IndexWriterConfig(analyzer));
// Responsible for creating the prefix string / geohash / token to identify the date.
// aka Create post codes
DateRangePrefixTree prefixTree = new DateRangePrefixTree(DateRangePrefixTree.JAVA_UTIL_TIME_COMPAT_CAL);
// Strategy indexing the token.
// aka transform post codes into tokens that make them efficient to search.
PrefixTreeStrategy strategy = new NumberRangePrefixTreeStrategy(prefixTree, TIME);
createDocument(w, "Bill", new Date(2017,1,1), prefixTree, strategy);
createDocument(w, "Ted", new Date(2018,1,1), prefixTree, strategy);
w.close();
// Written the document, now try query them
DirectoryReader reader;
try {
QueryParser queryParser = new QueryParser(NAME, analyzer);
System.out.println(queryParser.getLocale());
// Surely searching only on year for the easiest case should work?
Query q = queryParser.parse("time:[1972 TO 4018]");
// The following query returns 1 result, so Lucene is set up.
// Query q = queryParser.parse("name:Ted");
reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
int hitsPerPage = 10;
searcher.search(q, hitsPerPage);
TopDocs docs = searcher.search(q, hitsPerPage);
ScoreDoc[] hits = docs.scoreDocs;
// Hit count is zero and no document printed!!
// Putting a dependency on mockito would make this code harder to paste and run.
System.out.println("Hit count : "+hits.length);
for (int i = 0; i < hits.length; ++i) {
System.out.println(searcher.doc(hits[i].doc));
}
reader.close();
}
catch (ParseException e) {
e.printStackTrace();
}
}
private void createDocument(IndexWriter w, String name, Date fromDate, DateRangePrefixTree prefixTree, PrefixTreeStrategy strategy) throws IOException {
Document doc = new Document();
// Store a text/stored field for the name. This helps indicate that Lucene is orking.
doc.add(new TextField(NAME, name, Field.Store.YES));
//offset toDate
Calendar cal = Calendar.getInstance();
cal.setTime( fromDate );
cal.add( Calendar.DATE, 1 );
Date toDate = cal.getTime();
// This lets the prefix tree create whatever tokens it needs
// perhaps index year, date, second etc separately, hence multiple potential tokens.
for (IndexableField field : strategy.createIndexableFields(prefixTree.toRangeShape(
prefixTree.toUnitShape(fromDate), prefixTree.toUnitShape(toDate)))) {
// Debugging the tokens produced is difficult as I can't intuitively look at them and know if they are valid.
doc.add(field);
}
w.addDocument(doc);
}
}
更新:
与 StandardAnalyzer 相比,我认为答案可能是使用 SimpleAnalyzer,但这似乎也不起作用。
我对能够解析用户日期范围的要求似乎确实是 catered by SOLR,所以我希望这基于 Lucene 功能。
【问题讨论】:
-
我想也许答案是使用 SimpleAnalyzer 与 StandardAnalyzer 相比,但这似乎也不起作用。