问题描述
在一个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)}
有时候,您有时必须实现自己的缓存,例如何时需要将IndexReader与TaxonomyReader同步. IndexSearcher描述有关如何操作的建议做到这一点(通过 DirectoryReader.open(IndexWriter,applyAllDeletes)从用于提交更改的作家那里创建一个新读者).
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的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!