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?
NOTE: IndexWriter instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently. If your application requires external synchronization, you should not synchronize on the IndexWriter instance as this may cause deadlock; use your own (non-Lucene) objects instead.
An IndexWriter creates and maintains an index. The IndexWriterConfig. OpenMode option on IndexWriterConfig. setOpenMode(OpenMode) determines whether a new index is created, or whether an existing index is opened. Note that you can open an index with IndexWriterConfig.
Internally, Lucene refers to documents by an integer document number. The first document added to an index is numbered zero, and each subsequent document added gets a number one greater than the previous. Note that a document's number may change, so caution should be taken when storing these numbers outside of Lucene.
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.
According 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With