KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestMultiSearcher


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
33
34 /**
35  * Tests {@link MultiSearcher} class.
36  *
37  * @version $Id: TestMultiSearcher.java,v 1.6 2004/03/02 13:09:57 otis Exp $
38  */

39 public class TestMultiSearcher extends TestCase
40 {
41     public TestMultiSearcher(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     /**
47      * ReturnS a new instance of the concrete MultiSearcher class
48      * used in this test.
49      */

50     protected MultiSearcher getMultiSearcherInstance(Searcher[] searchers) throws IOException JavaDoc {
51         return new MultiSearcher(searchers);
52     }
53
54     public void testEmptyIndex()
55         throws Exception JavaDoc
56     {
57         // creating two directories for indices
58
Directory indexStoreA = new RAMDirectory();
59         Directory indexStoreB = new RAMDirectory();
60
61         // creating a document to store
62
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         // creating a document to store
68
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         // creating a document to store
74
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         // creating an index writer for the first index
80
IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(), true);
81         // creating an index writer for the second index, but writing nothing
82
IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), true);
83
84         //--------------------------------------------------------------------
85
// scenario 1
86
//--------------------------------------------------------------------
87

88         // writing the documents to the first index
89
writerA.addDocument(lDoc);
90         writerA.addDocument(lDoc2);
91         writerA.addDocument(lDoc3);
92         writerA.optimize();
93         writerA.close();
94
95         // closing the second index
96
writerB.close();
97
98         // creating the query
99
Query query = QueryParser.parse("handle:1", "fulltext", new StandardAnalyzer());
100
101         // building the searchables
102
Searcher[] searchers = new Searcher[2];
103         // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
104
searchers[0] = new IndexSearcher(indexStoreB);
105         searchers[1] = new IndexSearcher(indexStoreA);
106         // creating the multiSearcher
107
Searcher mSearcher = getMultiSearcherInstance(searchers);
108         // performing the search
109
Hits hits = mSearcher.search(query);
110
111         assertEquals(3, hits.length());
112
113         try {
114             // iterating over the hit documents
115
for (int i = 0; i < hits.length(); i++) {
116                 Document d = hits.doc(i);
117             }
118         }
119         catch (ArrayIndexOutOfBoundsException JavaDoc e)
120         {
121             fail("ArrayIndexOutOfBoundsException thrown: " + e.getMessage());
122             e.printStackTrace();
123         } finally{
124             mSearcher.close();
125         }
126
127
128         //--------------------------------------------------------------------
129
// scenario 2
130
//--------------------------------------------------------------------
131

132         // adding one document to the empty index
133
writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
134         writerB.addDocument(lDoc);
135         writerB.optimize();
136         writerB.close();
137
138         // building the searchables
139
Searcher[] searchers2 = new Searcher[2];
140         // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
141
searchers2[0] = new IndexSearcher(indexStoreB);
142         searchers2[1] = new IndexSearcher(indexStoreA);
143         // creating the mulitSearcher
144
Searcher mSearcher2 = getMultiSearcherInstance(searchers2);
145         // performing the same search
146
Hits hits2 = mSearcher2.search(query);
147
148         assertEquals(4, hits2.length());
149
150         try {
151             // iterating over the hit documents
152
for (int i = 0; i < hits2.length(); i++) {
153                 // no exception should happen at this point
154
Document d = hits2.doc(i);
155             }
156         }
157         catch (Exception JavaDoc e)
158         {
159             fail("Exception thrown: " + e.getMessage());
160             e.printStackTrace();
161         } finally{
162             mSearcher2.close();
163         }
164
165         //--------------------------------------------------------------------
166
// scenario 3
167
//--------------------------------------------------------------------
168

169         // deleting the document just added, this will cause a different exception to take place
170
Term term = new Term("id", "doc1");
171         IndexReader readerB = IndexReader.open(indexStoreB);
172         readerB.delete(term);
173         readerB.close();
174
175         // optimizing the index with the writer
176
writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
177         writerB.optimize();
178         writerB.close();
179
180         // building the searchables
181
Searcher[] searchers3 = new Searcher[2];
182
183         searchers3[0] = new IndexSearcher(indexStoreB);
184         searchers3[1] = new IndexSearcher(indexStoreA);
185         // creating the mulitSearcher
186
Searcher mSearcher3 = getMultiSearcherInstance(searchers3);
187         // performing the same search
188
Hits hits3 = mSearcher3.search(query);
189
190         assertEquals(3, hits3.length());
191
192         try {
193             // iterating over the hit documents
194
for (int i = 0; i < hits3.length(); i++) {
195                 Document d = hits3.doc(i);
196             }
197         }
198         catch (IOException JavaDoc e)
199         {
200             fail("IOException thrown: " + e.getMessage());
201             e.printStackTrace();
202         } finally{
203             mSearcher3.close();
204         }
205     }
206 }
207
Popular Tags