Searcher[] searchers = new Searcher[2];
// VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
searchers[0] = new IndexSearcher(indexStoreB);
searchers[1] = new IndexSearcher(indexStoreA);
// creating the multiSearcher
Searcher mSearcher = new MultiSearcher(searchers);
// performing the search
Hits hits = mSearcher.search(query);
assertEquals(3, hits.length());
try {
// iterating over the hit documents
for (int i = 0; i < hits.length(); i++) {
Document d = hits.doc(i);
}
}
catch (ArrayIndexOutOfBoundsException e)
{
fail("ArrayIndexOutOfBoundsException thrown: " + e.getMessage());
e.printStackTrace();
} finally{
mSearcher.close();
}
//--------------------------------------------------------------------
// scenario 2
//--------------------------------------------------------------------
// adding one document to the empty index
writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
writerB.addDocument(lDoc);
writerB.optimize();
writerB.close();
// building the searchables
Searcher[] searchers2 = new Searcher[2];
// VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
searchers2[0] = new IndexSearcher(indexStoreB);
searchers2[1] = new IndexSearcher(indexStoreA);
// creating the mulitSearcher
Searcher mSearcher2 = new MultiSearcher(searchers2);
// performing the same search
Hits hits2 = mSearcher2.search(query);
assertEquals(4, hits2.length());
try {
// iterating over the hit documents
for (int i = 0; i < hits2.length(); i++) {
// no exception should happen at this point
Document d = hits2.doc(i);
}
}
catch (Exception e)
{
fail("Exception thrown: " + e.getMessage());
e.printStackTrace();
} finally{
mSearcher2.close();
}
//--------------------------------------------------------------------
// scenario 3
//--------------------------------------------------------------------
// deleting the document just added, this will cause a different exception to take place
Term term = new Term("id", "doc1");
IndexReader readerB = IndexReader.open(indexStoreB);
readerB.delete(term);
readerB.close();
// optimizing the index with the writer
writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
writerB.optimize();
writerB.close();
// building the searchables
Searcher[] searchers3 = new Searcher[2];
searchers3[0] = new IndexSearcher(indexStoreB);
searchers3[1] = new IndexSearcher(indexStoreA);
// creating the mulitSearcher
Searcher mSearcher3 = new MultiSearcher(searchers3);
// performing the same search
Hits hits3 = mSearcher3.search(query);
assertEquals(3, hits3.length());
try {
// iterating over the hit documents
for (int i = 0; i < hits3.length(); i++) {
Document d = hits3.doc(i);
}
}
catch (IOException e)
{
fail("IOException thrown: " + e.getMessage());
e.printStackTrace();
} finally{
mSearcher3.close();
}
}