保留 Lucene IndexWriter & 是一个好习惯吗? IndexSearcher 在应用程序的生命周期内开放
Lucene 文档中指出,在应用程序中使用 IndexWriter 和 IndexSearcher 的一个实例是最快的。
目前,我有一个始终打开的 IndexWriter
静态实例,以及始终保持打开状态的 IndexSearcher
静态实例,但在 IndexWriter
对索引执行任何 CRUD 操作。我在索引管理类上实现了一个 Dispose 方法,该方法在应用程序结束时关闭 IndexWriter
和 IndexSearcher
(但它是一个 Web 应用程序,因此这可能需要运行数月)而不被调用)。
这听起来是合理的做事方式吗?而且使用静态实例是否会带来多线程问题......?即,我应该在使用时锁定我的编写器和搜索器吗?
It states in the Lucene documentation that it is fastest to use one instance of an IndexWriter and IndexSearcher across an application.
At the moment I have a static instance of IndexWriter
open at all times, and a static instance of IndexSearcher
that is kept open at all times but rebuilt when if the IndexWriter
performs any CRUD operations on the index. I have implemented a Dispose method on my index management class that closes both the IndexWriter
and IndexSearcher
when the application ends (however it is a web app so this is potentially months of running without being called).
Does that sound like reasonable way to go about doing things? And also does using static instances present problems with multi-threading..? I.e. should I be locking my writer and searcher when in use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Lucene 索引编写器、读取器和搜索器是线程安全的(请参阅 IndexWriter 文档的第二个注释 例如或 IndexSearcher 文档的第 1 部分),因此在多个线程中重用相同的实例是没有问题的。
根据如何管理索引编写器和搜索器的描述,我认为您正在重新实现 Lucene 的两个实用程序类,您可能会发现它们很有帮助: NRTManager 和 SearcherManager 这使得管理非常容易近乎实时的搜索者。
Lucene index writers, readers and searchers are thread-safe (see the 2nd note of the doc of IndexWriter for example or the 1st of the doc of IndexSearcher), so there is no problem reusing the same instances across multiple threads.
According to the description of how you manage index writers and searchers, I think you are re-implementing two utility classes of Lucene that you may find helpful: NRTManager and SearcherManager which make it very easy to manage near-realtime searchers.