我正在使用 Lucene 6.6.0,我想使用 Lucene 的近实时搜索功能。但是,我无法实现它。我尝试获取该功能的方式如下:
我初始化一个 IndexReader 实例:
this.reader = DirectoryReader.open(this.directory);
我们假设通过 IndexWriter 实例对索引进行了一些更改。然后,如果我理解正确的话,我需要 IndexReader 的第二个实例来提交更新:
this.newReader = DirectoryReader.openIfChanged(this.reader);
if (this.newReader != null) {
// Update the IndexSearcher with the new IndexReader instance
this.searcher = new IndexSearcher(this.newReader);
this.reader.close();
}
这里的问题是代码由于以下错误而无法编译:The method openIfChanged(DirectoryReader) in the type DirectoryReader is not applicable for the arguments (IndexReader)
.
我应该如何更新IndexReader
then ?
其次,如果我再次更新索引,我将需要另一个 IndexReader 实例,不是吗?在程序执行期间自由更新索引的最佳方法是否是在每次更新后在 2 个 IndexReader 实例之间切换?
谢谢。
尝试使用 SearcherManager 而不是 IndexReader:http://lucene.apache.org/core/6_6_0/core/org/apache/lucene/search/SearcherManager.html
基于SearcherManager,您可以执行以下方法:
// get a IndexSearcher for searching
IndexSearcher searcher = searcherManager.aquire();
// release IndexSearcher after search
searcherManager.release(searcher);
// refresh and add new index records to next search. usually after a commit
searcherManager.maybeRefresh();
我也尝试实现这一点,基本上我是这样做的:
- 创建一个 IndexWriter 并将其保持打开状态
- 创建一个以 IndexWriter 作为参数的 SearcherManager。
- 使用SearcherManager进行搜索
- 使用 IndexWriter 进行索引操作。
- 索引后提交
此外,您可以使用单独的线程定期提交,而不是在每次写入时提交,因为提交操作可能非常“昂贵”。
这里的例子:http://www.lucenetutorial.com/lucene-nrt-hello-world.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)