KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.document.Document;
23
24 /** An abstract base class for search implementations.
25  * Implements the main search methods.
26  *
27  * <p>Note that you can only access Hits from a Searcher as long as it is
28  * not yet closed, otherwise an IOException will be thrown.
29  */

30 public abstract class Searcher implements Searchable {
31
32   /** Returns the documents matching <code>query</code>.
33    * @throws BooleanQuery.TooManyClauses
34    */

35   public final Hits search(Query query) throws IOException JavaDoc {
36     return search(query, (Filter)null);
37   }
38
39   /** Returns the documents matching <code>query</code> and
40    * <code>filter</code>.
41    * @throws BooleanQuery.TooManyClauses
42    */

43   public Hits search(Query query, Filter filter) throws IOException JavaDoc {
44     return new Hits(this, query, filter);
45   }
46
47   /** Returns documents matching <code>query</code> sorted by
48    * <code>sort</code>.
49    * @throws BooleanQuery.TooManyClauses
50    */

51   public Hits search(Query query, Sort sort)
52     throws IOException JavaDoc {
53     return new Hits(this, query, null, sort);
54   }
55
56   /** Returns documents matching <code>query</code> and <code>filter</code>,
57    * sorted by <code>sort</code>.
58    * @throws BooleanQuery.TooManyClauses
59    */

60   public Hits search(Query query, Filter filter, Sort sort)
61     throws IOException JavaDoc {
62     return new Hits(this, query, filter, sort);
63   }
64
65   /** Expert: Low-level search implementation with arbitrary sorting. Finds
66    * the top <code>n</code> hits for <code>query</code>, applying
67    * <code>filter</code> if non-null, and sorting the hits by the criteria in
68    * <code>sort</code>.
69    *
70    * <p>Applications should usually call {@link
71    * Searcher#search(Query,Filter,Sort)} instead.
72    * @throws BooleanQuery.TooManyClauses
73    */

74   public TopFieldDocs search(Query query, Filter filter, int n,
75                              Sort sort) throws IOException JavaDoc {
76     return search(createWeight(query), filter, n, sort);
77   }
78
79   /** Lower-level search API.
80    *
81    * <p>{@link HitCollector#collect(int,float)} is called for every non-zero
82    * scoring document.
83    *
84    * <p>Applications should only use this if they need <i>all</i> of the
85    * matching documents. The high-level search API ({@link
86    * Searcher#search(Query)}) is usually more efficient, as it skips
87    * non-high-scoring hits.
88    * <p>Note: The <code>score</code> passed to this method is a raw score.
89    * In other words, the score will not necessarily be a float whose value is
90    * between 0 and 1.
91    * @throws BooleanQuery.TooManyClauses
92    */

93   public void search(Query query, HitCollector results)
94     throws IOException JavaDoc {
95     search(query, (Filter)null, results);
96   }
97
98   /** Lower-level search API.
99    *
100    * <p>{@link HitCollector#collect(int,float)} is called for every non-zero
101    * scoring document.
102    * <br>HitCollector-based access to remote indexes is discouraged.
103    *
104    * <p>Applications should only use this if they need <i>all</i> of the
105    * matching documents. The high-level search API ({@link
106    * Searcher#search(Query)}) is usually more efficient, as it skips
107    * non-high-scoring hits.
108    *
109    * @param query to match documents
110    * @param filter if non-null, a bitset used to eliminate some documents
111    * @param results to receive hits
112    * @throws BooleanQuery.TooManyClauses
113    */

114   public void search(Query query, Filter filter, HitCollector results)
115     throws IOException JavaDoc {
116     search(createWeight(query), filter, results);
117   }
118
119   /** Expert: Low-level search implementation. Finds the top <code>n</code>
120    * hits for <code>query</code>, applying <code>filter</code> if non-null.
121    *
122    * <p>Called by {@link Hits}.
123    *
124    * <p>Applications should usually call {@link Searcher#search(Query)} or
125    * {@link Searcher#search(Query,Filter)} instead.
126    * @throws BooleanQuery.TooManyClauses
127    */

128   public TopDocs search(Query query, Filter filter, int n)
129     throws IOException JavaDoc {
130     return search(createWeight(query), filter, n);
131   }
132
133   /** Returns an Explanation that describes how <code>doc</code> scored against
134    * <code>query</code>.
135    *
136    * <p>This is intended to be used in developing Similarity implementations,
137    * and, for good performance, should not be displayed with every hit.
138    * Computing an explanation is as expensive as executing the query over the
139    * entire index.
140    */

141   public Explanation explain(Query query, int doc) throws IOException JavaDoc {
142     return explain(createWeight(query), doc);
143   }
144
145   /** The Similarity implementation used by this searcher. */
146   private Similarity similarity = Similarity.getDefault();
147
148   /** Expert: Set the Similarity implementation used by this Searcher.
149    *
150    * @see Similarity#setDefault(Similarity)
151    */

152   public void setSimilarity(Similarity similarity) {
153     this.similarity = similarity;
154   }
155
156   /** Expert: Return the Similarity implementation used by this Searcher.
157    *
158    * <p>This defaults to the current value of {@link Similarity#getDefault()}.
159    */

160   public Similarity getSimilarity() {
161     return this.similarity;
162   }
163
164   /**
165    * creates a weight for <code>query</code>
166    * @return new weight
167    */

168   protected Weight createWeight(Query query) throws IOException JavaDoc {
169       return query.weight(this);
170   }
171
172   // inherit javadoc
173
public int[] docFreqs(Term[] terms) throws IOException JavaDoc {
174     int[] result = new int[terms.length];
175     for (int i = 0; i < terms.length; i++) {
176       result[i] = docFreq(terms[i]);
177     }
178     return result;
179   }
180
181   /* The following abstract methods were added as a workaround for GCJ bug #15411.
182    * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15411
183    */

184   abstract public void search(Weight weight, Filter filter, HitCollector results) throws IOException JavaDoc;
185   abstract public void close() throws IOException JavaDoc;
186   abstract public int docFreq(Term term) throws IOException JavaDoc;
187   abstract public int maxDoc() throws IOException JavaDoc;
188   abstract public TopDocs search(Weight weight, Filter filter, int n) throws IOException JavaDoc;
189   abstract public Document doc(int i) throws IOException JavaDoc;
190   abstract public Query rewrite(Query query) throws IOException JavaDoc;
191   abstract public Explanation explain(Weight weight, int doc) throws IOException JavaDoc;
192   abstract public TopFieldDocs search(Weight weight, Filter filter, int n, Sort sort) throws IOException JavaDoc;
193   /* End patch for GCJ bug #15411. */
194 }
195
Popular Tags