IndexReader。使用过程中有时会出现document被删除,reader还是原来的reader没有改变,所以使用openifchanged保证,
又因为IndexReader 初始化很耗费资源所以放在静态代码块里
private static Directory directory = null;
private static IndexReader reader = null;
static {
try {
directory = FSDirectory.open(new File("E:/lucene/index02"));
reader = IndexReader.open(directory);
} catch (Exception e) {
e.printStackTrace();
}
}
public IndexSearcher getIndexSearcher() {
try {
IndexReader tr = IndexReader.openIfChanged(reader);
if (tr != null) {
reader.close();
reader = tr;
}
return new IndexSearcher(reader);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}