0

I'm developing an "indexing service" (as part of a WPF application) that will run on a background thread, and use a FileSystemWatcher to monitor some files. When the file contents change I want to extract some information and update a Lucene index. Occasionally the user may wish to perform a search against the index.

Should I create an index reader every time the user performs a search? And a writer every time the indexes are updated? Or is it okay for my indexing service class (which is a singleton) to have singleton reader and writer instances (plus a singleton FSDirectory, that they both have a dependency on)?

If I was to use singleton instances, do I need to worry about closing/disposing them? If so, should my indexing service implement IDisposable, and do the cleanup in Dispose()?

4

2 回答 2

1

LuceneInderWriterIndexReader实例都是线程安全的:将它们作为单例重用应该不会有任何问题。初始化任何一个的新实例都非常昂贵(当为编写和搜索做出类似的设计选择时,有一个显着的改进,后者接近 60%)。

对于IndexWriter简单地保留您通过IndexingService. 为此,IndexReader您需要一种可以处理索引更改的不同方法。根据来源,您可以这样做,

public class IndexingService

   ...

   public IndexReader GetReader()
   {
       var reopenedReader = this.reader.Reopen(openReadOnly: true);
       if (reopenedReader != this.reader) 
       {
           this.reader.Dispose(); 
           return this.reader = reopenedReader;
       }

       return this.reader;
   }

调用this.reader.Reopen将首先检查阅读器是否是当前的(如果自打开后没有发生任何更改),如果是,它将简单地返回自己。如果发生了变化,那么阅读器将只加载发生变化的段,这通常比加载所有段更快。如果您期望多线程访问,您还需要同步对阅读器的访问。

Dispose()最后一件事:当应用程序关闭时,您肯定需要处理资源、编写器和读取器(使用该方法)。不这样做可能会损坏索引。

于 2013-10-05T03:53:07.717 回答
0

每次都创建一个 writer 并不是最佳选择,因此您最好等待一段时间并编写所有更改。您也可以在用户执行搜索时推送更改,这实际上取决于您的索引编写的复杂程度。

当您提交对索引的更改时,您需要创建一个新的索引阅读器,否则它不会获取更改。因此,我只会在您的索引例程中重新创建索引阅读器。但是除非您更新了索引,否则不要重新创建阅读器,因为这会减慢您的应用程序的速度。

于 2013-10-04T13:06:11.760 回答