Reputation: 113
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.
IndexWriter#commit
whenever a new index is built.SearcherManager
to 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. IndexWriter
or SearcherManager
throughout the app's lifetime.Now for the questions,
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 ?SearcherManager
instance using an IndexWriter
, creating it using a DirectoryReader
or creating it using a Directory
?Upvotes: 0
Views: 1195
Reputation: 1579
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.
Upvotes: 1