Playing around with Lucene. Trying to make my changes visible for other reading threads. Without rebuilding index. For that purpose i use SearcherManager class.
Creation of manager
Directory index = new SimpleFSDirectory(new File(LUCENE_INDEX_PATH));
w = new IndexWriter(index, config);
indexReader = IndexReader.open(w, true);
manager = new SearcherManager(w, true, null, null);
Update request
w.updateDocument(t, document);
manager.maybeReopen(); // openIfChanged same behavior
w.commit();
Search request
IndexSearcher searcher = manager.acquire();
try {
return performSearch(query, searcher, skip, limit);
} finally {
manager.release(searcher);
searcher = null;
}
Changes are flushed to disc, but new search request see them only after application restart (recreation of index). Looks like IndexSearcher still point to old Index.
You don’t need to open your own IndexReader; just create the SearcherManager (from your IndexWriter) then use acquire/release from it, to get a searcher/reader.
After adding/deleting docs with the writer, you should call maybeReopen, and then the next time you call SearcherManager.acquire the returned searcher will reflect the changes. It’s best to use a background thread (ie, not a thread doing searching) to index docs and call maybeReopen.
You don’t need to call IndexWriter.commit to make changes visible — only call this when you require durability (ie, that all changes are safely on disk and will survive OS/JVM crash, power loss, kill -9, etc.).