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?
views:
129answers:
1
+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:
- 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.
- 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
- Wait 1 minute for search queries to the old index files to complete and drop files from the old directory.
Yaroslav
2010-02-08 06:47:01
What is the reason I cannot use it in code? Does the locking mechanism change every other version? Or is there another reason?
soulmerge
2010-02-11 10:25:31
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
2010-02-11 15:13:56