Фон
Я предполагаю, что следующий код является полностью потокобезопасным:
// 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();
}
Следующий код вызывается из поискового сервлета:
// 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 ...
}
Вопросы
Я пытаюсь найти лучший / правильный способ реализовать это, чтобы избежать проблем с многопоточностью:
- Должен ли объект
FSDirectory dir
использоваться как статическая переменная?
- Должны ли объекты
IndexSearcher searcher
или IndexReader indexReader
быть сохранены как статическая переменная, а затем просто заменены при перестроении индекса?