views:

129

answers:

1

I am writing a wrapper around Zend's lucene implementation and wanted to add a function rebuildIndex() which reads all relevant fields from the database and re-creates the index file in a temporary folder. When the operation is finished, I want to replace the original folder with the new one. How can I lock the original lucene folder while replacing its contents? I haven't found anything in Zend's API docs, but I had read somewhere that locking works with files in lucene. Which folders/files do I need?

+3  A: 

Lucene use locking internally to maintain index consistency, so you can not use it in your code. I'd suggest using the following strategy:

  1. Create directory 'indexes' which contains directories for 2 different versions of index, e.g. 'index1' and 'index2' and a symlink 'current' to the index that should be used for searches.
  2. When updating index you drop files in inactive index directory, re-create the index and when it's done set 'current' to the newly indexed directory
  3. Wait 1 minute for search queries to the old index files to complete and drop files from the old directory.
Yaroslav
What is the reason I cannot use it in code? Does the locking mechanism change every other version? Or is there another reason?
soulmerge
The locking mechanism is used internally by lucene. You can use it but it would require very good understanding of lucene internals. It's not so trivial and needs very thorough testing.In your case more reliable solution is to just change the folder you pass to Zend_Search_Lucene::open function. Anyway index updates require reopening index reader.
Yaroslav