Using IndexWriter with SearchManager
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.
- 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#commitwhenever a new index is built.
- I'm using
SearcherManagerto acquire and release
IndexSearcherinstances for each search request. After the index is periodically built, I'm planning to use
SearcherManager#maybeRefreshmethod to get refreshed
IndexSearcherinstances.SearcherManager instance is also created once during the startup and I intend to maintain it through out.
- I do not close the
SearcherManagerthroughout the app's lifetime.
Now for the questions,
- If I create a new IndexWriter every time I need to rebuild the index, will
SearcherManager#maybeRefreshbe able to detect that it's a new IndexWriter Instance? Or do I need to create a new SearcherManager using the newly created IndexWriter ?
- What's the difference between creating a
SearcherManagerinstance using an
IndexWriter, creating it using a
DirectoryReaderor creating it using a
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 发布者: 28.12.2017 03:23