KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.index.Term;
27
28 /** Implements search over a set of <code>Searchables</code>.
29  *
30  * <p>Applications usually need only call the inherited {@link #search(Query)}
31  * or {@link #search(Query,Filter)} methods.
32  */

33 public class MultiSearcher extends Searcher {
34     /**
35      * Document Frequency cache acting as a Dummy-Searcher.
36      * This class is no full-fledged Searcher, but only supports
37      * the methods necessary to initialize Weights.
38      */

39   private static class CachedDfSource extends Searcher {
40     private Map JavaDoc dfMap; // Map from Terms to corresponding doc freqs
41
private int maxDoc; // document count
42

43     public CachedDfSource(Map JavaDoc dfMap, int maxDoc) {
44       this.dfMap = dfMap;
45       this.maxDoc = maxDoc;
46     }
47
48     public int docFreq(Term term) {
49       int df;
50       try {
51         df = ((Integer JavaDoc) dfMap.get(term)).intValue();
52       } catch (NullPointerException JavaDoc e) {
53         throw new IllegalArgumentException JavaDoc("df for term " + term.text()
54             + " not available");
55       }
56       return df;
57     }
58
59     public int[] docFreqs(Term[] terms) {
60       int[] result = new int[terms.length];
61       for (int i = 0; i < terms.length; i++) {
62         result[i] = docFreq(terms[i]);
63       }
64       return result;
65     }
66
67     public int maxDoc() {
68       return maxDoc;
69     }
70
71     public Query rewrite(Query query) {
72       // this is a bit of a hack. We know that a query which
73
// creates a Weight based on this Dummy-Searcher is
74
// always already rewritten (see preparedWeight()).
75
// Therefore we just return the unmodified query here
76
return query;
77     }
78
79     public void close() {
80       throw new UnsupportedOperationException JavaDoc();
81     }
82
83     public Document doc(int i) {
84       throw new UnsupportedOperationException JavaDoc();
85     }
86
87     public Explanation explain(Weight weight,int doc) {
88       throw new UnsupportedOperationException JavaDoc();
89     }
90
91     public void search(Weight weight, Filter filter, HitCollector results) {
92       throw new UnsupportedOperationException JavaDoc();
93     }
94
95     public TopDocs search(Weight weight,Filter filter,int n) {
96       throw new UnsupportedOperationException JavaDoc();
97     }
98
99     public TopFieldDocs search(Weight weight,Filter filter,int n,Sort sort) {
100       throw new UnsupportedOperationException JavaDoc();
101     }
102   };
103
104
105   private Searchable[] searchables;
106   private int[] starts;
107   private int maxDoc = 0;
108
109   /** Creates a searcher which searches <i>searchables</i>. */
110   public MultiSearcher(Searchable[] searchables) throws IOException JavaDoc {
111     this.searchables = searchables;
112
113     starts = new int[searchables.length + 1]; // build starts array
114
for (int i = 0; i < searchables.length; i++) {
115       starts[i] = maxDoc;
116       maxDoc += searchables[i].maxDoc(); // compute maxDocs
117
}
118     starts[searchables.length] = maxDoc;
119   }
120   
121   /** Return the array of {@link Searchable}s this searches. */
122   public Searchable[] getSearchables() {
123     return searchables;
124   }
125
126   protected int[] getStarts() {
127     return starts;
128   }
129
130   // inherit javadoc
131
public void close() throws IOException JavaDoc {
132     for (int i = 0; i < searchables.length; i++)
133       searchables[i].close();
134   }
135
136   public int docFreq(Term term) throws IOException JavaDoc {
137     int docFreq = 0;
138     for (int i = 0; i < searchables.length; i++)
139       docFreq += searchables[i].docFreq(term);
140     return docFreq;
141   }
142
143   // inherit javadoc
144
public Document doc(int n) throws IOException JavaDoc {
145     int i = subSearcher(n); // find searcher index
146
return searchables[i].doc(n - starts[i]); // dispatch to searcher
147
}
148
149   /** Call {@link #subSearcher} instead.
150    * @deprecated
151    */

152   public int searcherIndex(int n) {
153     return subSearcher(n);
154   }
155
156   /** Returns index of the searcher for document <code>n</code> in the array
157    * used to construct this searcher. */

158   public int subSearcher(int n) { // find searcher for doc n:
159
// replace w/ call to Arrays.binarySearch in Java 1.2
160
int lo = 0; // search starts array
161
int hi = searchables.length - 1; // for first element less
162
// than n, return its index
163
while (hi >= lo) {
164       int mid = (lo + hi) >> 1;
165       int midValue = starts[mid];
166       if (n < midValue)
167     hi = mid - 1;
168       else if (n > midValue)
169     lo = mid + 1;
170       else { // found a match
171
while (mid+1 < searchables.length && starts[mid+1] == midValue) {
172           mid++; // scan to last match
173
}
174     return mid;
175       }
176     }
177     return hi;
178   }
179
180   /** Returns the document number of document <code>n</code> within its
181    * sub-index. */

182   public int subDoc(int n) {
183     return n - starts[subSearcher(n)];
184   }
185
186   public int maxDoc() throws IOException JavaDoc {
187     return maxDoc;
188   }
189
190   public TopDocs search(Weight weight, Filter filter, int nDocs)
191   throws IOException JavaDoc {
192
193     HitQueue hq = new HitQueue(nDocs);
194     int totalHits = 0;
195
196     for (int i = 0; i < searchables.length; i++) { // search each searcher
197
TopDocs docs = searchables[i].search(weight, filter, nDocs);
198       totalHits += docs.totalHits; // update totalHits
199
ScoreDoc[] scoreDocs = docs.scoreDocs;
200       for (int j = 0; j < scoreDocs.length; j++) { // merge scoreDocs into hq
201
ScoreDoc scoreDoc = scoreDocs[j];
202         scoreDoc.doc += starts[i]; // convert doc
203
if(!hq.insert(scoreDoc))
204             break; // no more scores > minScore
205
}
206     }
207
208     ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
209     for (int i = hq.size()-1; i >= 0; i--) // put docs in array
210
scoreDocs[i] = (ScoreDoc)hq.pop();
211     
212     float maxScore = (totalHits==0) ? Float.NEGATIVE_INFINITY : scoreDocs[0].score;
213     
214     return new TopDocs(totalHits, scoreDocs, maxScore);
215   }
216
217   public TopFieldDocs search (Weight weight, Filter filter, int n, Sort sort)
218   throws IOException JavaDoc {
219     FieldDocSortedHitQueue hq = null;
220     int totalHits = 0;
221
222     float maxScore=Float.NEGATIVE_INFINITY;
223     
224     for (int i = 0; i < searchables.length; i++) { // search each searcher
225
TopFieldDocs docs = searchables[i].search (weight, filter, n, sort);
226       
227       if (hq == null) hq = new FieldDocSortedHitQueue (docs.fields, n);
228       totalHits += docs.totalHits; // update totalHits
229
maxScore = Math.max(maxScore, docs.getMaxScore());
230       ScoreDoc[] scoreDocs = docs.scoreDocs;
231       for (int j = 0; j < scoreDocs.length; j++) { // merge scoreDocs into hq
232
ScoreDoc scoreDoc = scoreDocs[j];
233         scoreDoc.doc += starts[i]; // convert doc
234
if (!hq.insert (scoreDoc))
235           break; // no more scores > minScore
236
}
237     }
238
239     ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
240     for (int i = hq.size() - 1; i >= 0; i--) // put docs in array
241
scoreDocs[i] = (ScoreDoc) hq.pop();
242
243     return new TopFieldDocs (totalHits, scoreDocs, hq.getFields(), maxScore);
244   }
245
246
247   // inherit javadoc
248
public void search(Weight weight, Filter filter, final HitCollector results)
249     throws IOException JavaDoc {
250     for (int i = 0; i < searchables.length; i++) {
251
252       final int start = starts[i];
253
254       searchables[i].search(weight, filter, new HitCollector() {
255       public void collect(int doc, float score) {
256         results.collect(doc + start, score);
257       }
258     });
259
260     }
261   }
262
263   public Query rewrite(Query original) throws IOException JavaDoc {
264     Query[] queries = new Query[searchables.length];
265     for (int i = 0; i < searchables.length; i++) {
266       queries[i] = searchables[i].rewrite(original);
267     }
268     return queries[0].combine(queries);
269   }
270
271   public Explanation explain(Weight weight, int doc) throws IOException JavaDoc {
272     int i = subSearcher(doc); // find searcher index
273
return searchables[i].explain(weight,doc-starts[i]); // dispatch to searcher
274
}
275
276   /**
277    * Create weight in multiple index scenario.
278    *
279    * Distributed query processing is done in the following steps:
280    * 1. rewrite query
281    * 2. extract necessary terms
282    * 3. collect dfs for these terms from the Searchables
283    * 4. create query weight using aggregate dfs.
284    * 5. distribute that weight to Searchables
285    * 6. merge results
286    *
287    * Steps 1-4 are done here, 5+6 in the search() methods
288    *
289    * @return rewritten queries
290    */

291   protected Weight createWeight(Query original) throws IOException JavaDoc {
292     // step 1
293
Query rewrittenQuery = rewrite(original);
294
295     // step 2
296
Set JavaDoc terms = new HashSet JavaDoc();
297     rewrittenQuery.extractTerms(terms);
298
299     // step3
300
Term[] allTermsArray = new Term[terms.size()];
301     terms.toArray(allTermsArray);
302     int[] aggregatedDfs = new int[terms.size()];
303     for (int i = 0; i < searchables.length; i++) {
304       int[] dfs = searchables[i].docFreqs(allTermsArray);
305       for(int j=0; j<aggregatedDfs.length; j++){
306         aggregatedDfs[j] += dfs[j];
307       }
308     }
309
310     HashMap JavaDoc dfMap = new HashMap JavaDoc();
311     for(int i=0; i<allTermsArray.length; i++) {
312       dfMap.put(allTermsArray[i], new Integer JavaDoc(aggregatedDfs[i]));
313     }
314
315     // step4
316
int numDocs = maxDoc();
317     CachedDfSource cacheSim = new CachedDfSource(dfMap, numDocs);
318
319     return rewrittenQuery.weight(cacheSim);
320   }
321
322 }
323
Popular Tags