您是否每次都需要一个IndexReader和IndexSearcher的新实例? [英] Do you need a new instance of IndexReader and IndexSearcher every time?

查看:249
本文介绍了您是否每次都需要一个IndexReader和IndexSearcher的新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Web应用程序中,整个应用程序有一个IndexReader和一个分别的IndexSearcher.

In a web application, I'm having one IndexReader and one respective IndexSearcher for the whole application.

文档说它们是线程安全的,这样就可以了,但是如果基础索引发生了变化(例如IndexWriter进行了更改),它应该可以正常工作吗?

The documentation says they are thread-safe, so that's ok, but is it supposed to work properly if the underlying index changes (e.g. an IndexWriter makes changes)?

推荐答案

是的,您需要重新打开阅读器.

Yes, you need to reopen the reader.

使用 SearcherManager 来自动更新阅读器,调用进行更改后> maybeRefresh .

Use the SearcherManager to automatically update the reader, calling maybeRefresh when you've made changes.

下面的Scala代码示例:

Samples of Scala code below:

@volatile protected var LAST_SEARCHER_REFRESH = 0
protected lazy val SEARCHER_MANAGER = new SearcherManager (LUCENE_DIR, new SearcherFactory {
  override def newSearcher (reader: IndexReader): IndexSearcher = {new IndexSearcher (reader)}
})

...

if (LAST_SEARCHER_REFRESH != Time.relativeSeconds()) {LAST_SEARCHER_REFRESH = Time.relativeSeconds(); SEARCHER_MANAGER.maybeRefresh()}
val searcher = SEARCHER_MANAGER.acquire(); try {
  searcher.search (query, collector)
  ...
} finally {SEARCHER_MANAGER.release (searcher)}

有时候,您有时必须实现自己的缓存,例如 IndexSearcher描述有关如何操作的建议做到这一点(通过

Sometimes you'll have to implement your own caching though, like when you need to synchronize the IndexReader with TaxonomyReader. IndexSearcher description has recommendations about how to do that (there is a fast path via DirectoryReader.open(IndexWriter, applyAllDeletes) to make a new reader from a writer used to commit the changes).

这篇关于您是否每次都需要一个IndexReader和IndexSearcher的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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