1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.analysis.standard.StandardAnalyzer; 20 import org.apache.lucene.document.Document; 21 import org.apache.lucene.document.Field; 22 import org.apache.lucene.index.IndexReader; 23 import org.apache.lucene.index.IndexWriter; 24 import org.apache.lucene.index.Term; 25 import org.apache.lucene.queryParser.QueryParser; 26 import org.apache.lucene.search.Searcher; 27 import org.apache.lucene.store.Directory; 28 import org.apache.lucene.store.RAMDirectory; 29 30 import junit.framework.TestCase; 31 32 import java.io.IOException ; 33 34 39 public class TestMultiSearcher extends TestCase 40 { 41 public TestMultiSearcher(String name) 42 { 43 super(name); 44 } 45 46 50 protected MultiSearcher getMultiSearcherInstance(Searcher[] searchers) throws IOException { 51 return new MultiSearcher(searchers); 52 } 53 54 public void testEmptyIndex() 55 throws Exception 56 { 57 Directory indexStoreA = new RAMDirectory(); 59 Directory indexStoreB = new RAMDirectory(); 60 61 Document lDoc = new Document(); 63 lDoc.add(Field.Text("fulltext", "Once upon a time.....")); 64 lDoc.add(Field.Keyword("id", "doc1")); 65 lDoc.add(Field.Keyword("handle", "1")); 66 67 Document lDoc2 = new Document(); 69 lDoc2.add(Field.Text("fulltext", "in a galaxy far far away.....")); 70 lDoc2.add(Field.Keyword("id", "doc2")); 71 lDoc2.add(Field.Keyword("handle", "1")); 72 73 Document lDoc3 = new Document(); 75 lDoc3.add(Field.Text("fulltext", "a bizarre bug manifested itself....")); 76 lDoc3.add(Field.Keyword("id", "doc3")); 77 lDoc3.add(Field.Keyword("handle", "1")); 78 79 IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(), true); 81 IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), true); 83 84 88 writerA.addDocument(lDoc); 90 writerA.addDocument(lDoc2); 91 writerA.addDocument(lDoc3); 92 writerA.optimize(); 93 writerA.close(); 94 95 writerB.close(); 97 98 Query query = QueryParser.parse("handle:1", "fulltext", new StandardAnalyzer()); 100 101 Searcher[] searchers = new Searcher[2]; 103 searchers[0] = new IndexSearcher(indexStoreB); 105 searchers[1] = new IndexSearcher(indexStoreA); 106 Searcher mSearcher = getMultiSearcherInstance(searchers); 108 Hits hits = mSearcher.search(query); 110 111 assertEquals(3, hits.length()); 112 113 try { 114 for (int i = 0; i < hits.length(); i++) { 116 Document d = hits.doc(i); 117 } 118 } 119 catch (ArrayIndexOutOfBoundsException e) 120 { 121 fail("ArrayIndexOutOfBoundsException thrown: " + e.getMessage()); 122 e.printStackTrace(); 123 } finally{ 124 mSearcher.close(); 125 } 126 127 128 132 writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false); 134 writerB.addDocument(lDoc); 135 writerB.optimize(); 136 writerB.close(); 137 138 Searcher[] searchers2 = new Searcher[2]; 140 searchers2[0] = new IndexSearcher(indexStoreB); 142 searchers2[1] = new IndexSearcher(indexStoreA); 143 Searcher mSearcher2 = getMultiSearcherInstance(searchers2); 145 Hits hits2 = mSearcher2.search(query); 147 148 assertEquals(4, hits2.length()); 149 150 try { 151 for (int i = 0; i < hits2.length(); i++) { 153 Document d = hits2.doc(i); 155 } 156 } 157 catch (Exception e) 158 { 159 fail("Exception thrown: " + e.getMessage()); 160 e.printStackTrace(); 161 } finally{ 162 mSearcher2.close(); 163 } 164 165 169 Term term = new Term("id", "doc1"); 171 IndexReader readerB = IndexReader.open(indexStoreB); 172 readerB.delete(term); 173 readerB.close(); 174 175 writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false); 177 writerB.optimize(); 178 writerB.close(); 179 180 Searcher[] searchers3 = new Searcher[2]; 182 183 searchers3[0] = new IndexSearcher(indexStoreB); 184 searchers3[1] = new IndexSearcher(indexStoreA); 185 Searcher mSearcher3 = getMultiSearcherInstance(searchers3); 187 Hits hits3 = mSearcher3.search(query); 189 190 assertEquals(3, hits3.length()); 191 192 try { 193 for (int i = 0; i < hits3.length(); i++) { 195 Document d = hits3.doc(i); 196 } 197 } 198 catch (IOException e) 199 { 200 fail("IOException thrown: " + e.getMessage()); 201 e.printStackTrace(); 202 } finally{ 203 mSearcher3.close(); 204 } 205 } 206 } 207 | Popular Tags |