Search code examples
phplucenezend-search-lucene

locking a lucene folder


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?


Solution

  • 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.