Hi,
I've just migrated my webapp from Lucene 3.6 to 4.0-BETA. My 2 indexes are
updated every couple of minutes by a batch. The webapp searcher needs to
get refreshed whenever this happens. In 3.6, I was doing it this way:
private IndexSearcher getIndexSearcher() {
try {
if (is == null || !is.getIndexReader().isCurrent()) {
if (is != null) {
is.close();
}
IndexReader readers[] = new IndexReader[2];
for (int i = 0; i < 2; i++) {
readers[i] = IndexReader.open(MyFSDirectories.get(i));
}
is = new IndexSearcher(new MultiReader(readers));
}
} catch (Exception e) {
e.printStackTrace();
}
return is;
}
Now that the API changed in 4.0-BETA. I'm doing it this odd way:
private IndexSearcher getIndexSearcher() {
try {
boolean refresh = false;
if (is == null) {
refresh = true;
} else {
MultiReader ir = ((MultiReader) is.getIndexReader());
List<DirectoryReader> list = (List<DirectoryReader>)
ir.getSequentialSubReaders();
try {
if (!list.get(0).isCurrent() ||
!list.get(1).isCurrent()) {
refresh = true;
}
} catch (Exception e) {
e.printStackTrace();
refresh = true;
}
}
if (refresh) {
if (is != null) {
((MultiReader) is.getIndexReader()).close();
}
DirectoryReader readers[] = new DirectoryReader[2];
for (int i = 0; i < 2; i++) {
readers[i] =
DirectoryReader.open(MyFSDirectories.get(i));
}
is = new IndexSearcher(new MultiReader(readers));
}
} catch (Exception e) {
e.printStackTrace();
}
return is;
}
The problem is that sometimes isCurrent() returns an exception indicating
that the underlying IndexReader is closed.
My questions are:
1. How can I verify of the IndexReader is open before calling isCurrent();
2. Is there a better way to deal with MultiReaders?
Regards,
Mossaab