publicclass TestIndexReader {
privatestatic Directory directory = null;
privatestatic IndexReader indexReader = null;
static {
try {
directory = FSDirectory.open(new File("D:\\lucene\\index"));
} catch (IOException e) {
e.printStackTrace();
}
}
public IndexSearcher getSearcher() throws IOException{
if(indexReader==null)
indexReader = DirectoryReader.open(directory);
returnnew IndexSearcher(indexReader);
}
}
publicclass TestIndexReader {
privatestatic Directory directory = null;
privatestatic IndexReader indexReader = null;
static {
try {
directory = FSDirectory.open(new File("D:\\lucene\\index"));
} catch (IOException e) {
e.printStackTrace();
}
}
public IndexSearcher getSearcher() throws IOException{
if(indexReader==null)
indexReader = DirectoryReader.open(directory);
else{
IndexReader changeReader = DirectoryReader.openIfChanged((DirectoryReader)indexReader);
if(changeReader!=null){
indexReader.close();
indexReader = changeReader;
}
}
returnnew IndexSearcher(indexReader);
}
}