我正在使用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
呢?其次,如果我再次更新索引,我需要另一个
IndexReader
实例,对吗?在程序执行期间自由更新索引的最佳方法是在每次更新后在两个IndexReader
实例之间切换吗?谢谢。
this.writer.maybeMerge(); this.writer.commit(); this.searcherManager.maybeRefresh();
这三行代码是为了能够在更新后的索引中进行搜索,对吗?我更喜欢在索引之后手动调用这些方法来刷新索引,而不是使用线程,因为在我的情况下,索引只在开始时进行,所以没有必要定期刷新它。当索引是动态的时候,使用线程来刷新索引会更优,对吗? - C. Güzelhan