Lucene近实时搜索 [英] Lucene near real time search

查看:240
本文介绍了Lucene近实时搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Lucene 6.6.0,并且我想使用Lucene的近实时搜索功能.但是,我无法实现它.我尝试获得该功能的方法如下:

I am using Lucene 6.6.0 and I would like to use the near real-time search feature of Lucene. However, I could not manage to implement it. The way I try to get the feature is as follows:

我初始化一个IndexReader实例:

I initialize an IndexReader instance:

this.reader = DirectoryReader.open(this.directory);

我们假设通过IndexWriter实例对索引进行了一些更改.然后,如果我理解正确,则需要第二个IndexReader实例来提交更新:

Let's assume some changes have been made in the index via an IndexWriter instance. Then, if I understand correctly, I need a second instance of IndexReader to commit updates:

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).

The issue here is that the code does not compile because of the following error: The method openIfChanged(DirectoryReader) in the type DirectoryReader is not applicable for the arguments (IndexReader).

然后我应该如何更新IndexReader?

第二,如果我再次更新索引,则需要另一个IndexReader实例,不是吗?在程序执行期间自由更新索引的最佳方法是在每次更新后在2个IndexReader实例之间切换吗?

Secondly, if I update the index again, I will need another IndexReader instance, won't I ? Would the most optimal way to update the index freely during the execution of the program be by switching between 2 IndexReader instances after each update ?

谢谢.

推荐答案

尝试使用SearcherManager代替IndexReader: http://lucene.apache.org/core/6_6_0/core/org/apache/lucene/search/SearcherManager.html

Try to use a SearcherManager instead of a IndexReader: http://lucene.apache.org/core/6_6_0/core/org/apache/lucene/search/SearcherManager.html

基于SearcherManager,您可以执行以下方法:

Based on the SearcherManager your able to execute following methods:

// 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();

我也尝试实现这一点,基本上我做到了:

I tried to implement this as well and basically i did this:

  • 创建一个IndexWriter并保持打开状态
  • 使用IndexWriter作为参数创建SearcherManager.
  • 使用SearcherManager进行搜索
  • 使用IndexWriter进行索引操作.
  • 建立索引后提交

另外,您可以使用单独的线程来定期提交而不是在每次写入时提交,因为提交操作可能非常昂贵".

Additionally you can use a separate thread to commit periodically and not on every write because the commit operation may be pretty "expensive".

此处的示例: http://www.lucenetutorial.com/lucene- nrt-hello-world.html

这篇关于Lucene近实时搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆