使用Lucene spatial search/DateRangePrefixTree进行java日期范围查询?
我使用的是Lucene 6.3,但我无法找出以下非常基本的搜索查询的错误。它只需向每个文档添加一个日期范围,然后尝试在更大的范围内搜索,以便找到这两个文档。怎么了
这里有一些内联注释,可以让exmaple变得不言自明。如果有任何不清楚的地方,请告诉我
请注意,我的主要要求是能够执行日期范围查询以及其他字段查询,例如
text:interesting date:[2014 TO NOW]
这是在观看了Lucene spatial deep dive video的介绍之后,介绍了DateRangePrefixTree和策略所基于的框架
Rant:我觉得如果我在这里犯了任何错误,我应该会在查询或写作中得到一些验证错误,因为我的例子是多么简单
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);
}
}
更新:
我想答案可能是使用SimpleAnalyzer而不是StandardAnalyzer,但这似乎也不起作用
我对能够解析用户日期范围的要求似乎是catered by SOLR,所以我希望这是基于Lucene功能的
# 1 楼答案
首先,QueryParser可以解析日期,并默认生成TermRangeQuery。请参阅以下生成TermRangeQuery的默认解析器的方法
这假设您将在lucene数据库中以字符串形式存储日期,这有点低效,但只要使用SimpleAnalyzer或等效工具,就可以直接工作
或者,您可以将日期存储为LongPoint,这对于我上面的问题中的日期场景来说是最有效的,其中日期是一个时间点,每个字段存储一个日期
但就像DatePrefixTree建议的那样,这需要编写硬编码查询
即使在这里,如果以下方法被生成长点范围查询的版本覆盖,也可以重用QueryParser
对于datePrefix树版本也可以这样做,但只有在以下情况下,此方案才有价值:
对查询解析器进行调整,使其具有一个方便的术语,能够捕获我想象中的所有相关场景,对于最后一个案例来说,这将是相当多的工作
此外,请注意不要将日期(年、月、日)与GregoriaCalendar(年、月、日)混用,因为参数不相等,会导致问题
请参阅
java.util.Date#Date(int, int, int)
,了解参数之间的差异以及为什么不推荐使用此构造函数。根据问题中的代码,这让我大吃一惊再次感谢femtoRgon指出了空间搜索的机制,但最终这不是我要走的路
# 2 楼答案
QueryParser在搜索空间字段时不会有用,分析器也不会产生任何影响。分析器设计用于标记和转换文本。因此,它们不被空间场使用。类似地,QueryParser主要面向文本搜索,不支持空间查询
您需要使用空间查询进行查询。特别是AbstractPrefixTreeQuery的子类将非常有用
例如,如果我想查询其时间字段范围包含2003-2005年的文档,我可以创建如下查询:
因此,这将匹配已编制索引的文档,例如,范围为2000-01-01到2006-01-01
或者换一种方式,匹配范围完全在查询范围内的所有文档:
关于参数的注意:我不太理解这些查询的一些参数,尤其是detailLevel和prefixGridScanLevel。还没有找到任何关于它们是如何工作的文档。这些值似乎在我的基本测试中起作用,但我不知道最好的选择是什么