Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to keep a Lucene IndexWriter & IndexSearcher open for the lifetime of an app

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?

like image 748
jcvandan Avatar asked Feb 21 '12 12:02

jcvandan


People also ask

Is Lucene thread safe?

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.

What is IndexWriter?

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.

How Lucene works internally?

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.


1 Answers

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.

like image 119
jpountz Avatar answered Oct 12 '22 11:10

jpountz