I'm using Lucene 3.0.3. I've made a Spring bean aiming to encapsulate all operations on the same index.
public class IndexOperations {
private IndexWriter writer;
private IndexReader reader;
private IndexSearcher searcher;
public void init() {...}
public void destroy() {...}
public void save(Document d) {...}
public void delete(Document d) {...}
public List<Document> list() {...}
}
In order to permit fast changes and searches, I thought leaving writer, reader and searcher open could be a good idea. But the problem is that commited changes on writer can't be seen by readers until reopen. And this operation can be costly, so maybe is not a good idea for fast searches.
What would be the best approach for this typical scenario?
You should keep the writer always open, but don't persist the reader/searcher. When you need a searcher just do IndexSearcher srch = new IndexSearcher(writer.getReader());
This way the searcher will get the most recent changes, even if they aren't flushed to disk yet (giving you the best of both worlds).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With