Background
I am assuming the following code is completely thread safe:
// Called from a servlet when a user action results in the index needing to be updated
public static void rebuildIndex() {
FSDirectory dir = new NIOFSDirectory(new File(Configuration.getAttachmentFolder()), null);
IndexWriter w = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
.... build index ...
w.optimize();
w.commit();
w.close();
}
The following code is called from the search servlet:
// Called by the search servlet
public void search() {
FSDirectory dir = new NIOFSDirectory(new File(Configuration.getAttachmentFolder()), null);
IndexReader indexReader = IndexReader.open(dir,true);
IndexSearcher searcher = new IndexSearcher(indexReader);
.... do the search ...
}
Questions
I am trying to work out the best/correct way to implement this to avoid multi-threading problems:
- Should the
FSDirectory dir
object be shared as some sort of static variable? - Should the
IndexSearcher searcher
or theIndexReader indexReader
objects be stored as a static variable, and then simply replaced when the index is rebuilt?