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?
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.
ReplyDeleteAccording 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.