Using IndexWriter with SearchManager


302 观看


103 作者的声誉

I have a few basic questions regarding the usage of SearcherManager with IndexWriter.

I need to periodically re-build the Lucene index in the application and currently it happens on a different thread other than the one that serves the search requests.

  1. Can I use the same IndexWriter instance through the lifetime of the application to rebuild the index periodically? Currently, I create / open it once during the startup and just call IndexWriter#commit whenever a new index is built.
  2. I'm using SearcherManagerto acquire and release IndexSearcher instances for each search request. After the index is periodically built, I'm planning to use SearcherManager#maybeRefresh method to get refreshed IndexSearcher instances.SearcherManager instance is also created once during the startup and I intend to maintain it through out.
  3. I do not close the IndexWriter or SearcherManager throughout the app's lifetime.

Now for the questions,

  1. If I create a new IndexWriter every time I need to rebuild the index, will SearcherManager#maybeRefresh be able to detect that it's a new IndexWriter Instance? Or do I need to create a new SearcherManager using the newly created IndexWriter ?
  2. What's the difference between creating a SearcherManager instance using an IndexWriter, creating it using a DirectoryReader or creating it using a Directory ?
作者: Sarat 的来源 发布者: 2017 年 12 月 27 日

回应 1


1046 作者的声誉

The answers depend on how you construct your SearcherManager:

If you construct it with a DirectoryReader, all future IndexSearchers acquired from the SearcherManager will be based on that reader, i.e. all searches will provide results from the point in time you instantiated the SearcherManager. If you write data to the index/directory and run SearcherManager.maybeRefresh() afterwards, the reader will not be updated and your search results will be outdated.

If you construct the SearcherManager with an IndexWriter, SearcherManager.maybeRefresh() will update the SearcherManager's reader if data has been written and commited by the writer. All newly acquired IndexSearchers will then reflect the new state of the underlying index.

Despite having limited experience, I recommend using the latter approach. It provides a very simple way to implement near-real-time searching: At application start you create an IndexWriter and construct a SearcherManager with it. Afterwards you start a background thread that periodically commits all changes in the IndexWriter and refreshes the SearcherManager. For the lifetime of your application you can keep using the initial IndexWriter and SearcherManager without having to close/reopen them.

PS: I have only started working with Lucene a few days ago, so don't take everything I wrote here as 100% certain.

作者: xpages-noob 发布者: 2017 年 12 月 28 日