Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple IndexWriter and multiple cross-process IndexWriter

I am googling for two days... I really need help.

I have an application with multiple threads trying to update a lucene index using Open/Close of IndexWriter for each update. Threads can start at any time.

Yeah, the problem with write.lock! So there are may be two or more solutions:

1) check IndexWriter.IsLocked(index) and if it is locked to sleep the thread.
2) Open an IndexWriter and never close it. The problem is that I have another application using the same index. Also when should I close the index and finalize the whole process?

here are interesting postings
Lucene IndexWriter thread safety
Lucene - open a closed IndexWriter

Update: Exactly 2 years later I did a rest API which wraps this and all writes and reads were routed to the API.

like image 476
mynkow Avatar asked Sep 11 '12 17:09

mynkow


1 Answers

Multiple threads, same process:

Keep your IndexWriter opened, and share it across multiple thread. The IndexWriter is threadsafe.

For multiple processes, your solution #1 is prone to issues with race conditions. You would need to use named Mutex to implement it safely: http://msdn.microsoft.com/en-us/library/bwe34f1k.aspx

That being said, I'd personally opt for a process dedicated to writing to the index, and communicate with it using something like WCF.

like image 85
Jf Beaulac Avatar answered Oct 13 '22 09:10

Jf Beaulac