1 package org.apache.lucene.search;
2
3 /**
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.index.CorruptIndexException;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.document.Document;
25
26 /** An abstract base class for search implementations.
27 * Implements the main search methods.
28 *
29 * <p>Note that you can only access Hits from a Searcher as long as it is
30 * not yet closed, otherwise an IOException will be thrown.
31 */
32 public abstract class Searcher implements Searchable {
33
34 /** Returns the documents matching <code>query</code>.
35 * @throws BooleanQuery.TooManyClauses
36 * @deprecated Hits will be removed in Lucene 3.0. Use
37 * {@link #search(Query, Filter, int)} instead.
38 */
39 public final Hits search(Query query) throws IOException {
40 return search(query, (Filter)null);
41 }
42
43 /** Returns the documents matching <code>query</code> and
44 * <code>filter</code>.
45 * @throws BooleanQuery.TooManyClauses
46 * @deprecated Hits will be removed in Lucene 3.0. Use
47 * {@link #search(Query, Filter, int)} instead.
48 */
49 public Hits search(Query query, Filter filter) throws IOException {
50 return new Hits(this, query, filter);
51 }
52
53 /** Returns documents matching <code>query</code> sorted by
54 * <code>sort</code>.
55 * @throws BooleanQuery.TooManyClauses
56 * @deprecated Hits will be removed in Lucene 3.0. Use
57 * {@link #search(Query, Filter, int, Sort)} instead.
58 */
59 public Hits search(Query query, Sort sort)
60 throws IOException {
61 return new Hits(this, query, null, sort);
62 }
63
64 /** Returns documents matching <code>query</code> and <code>filter</code>,
65 * sorted by <code>sort</code>.
66 * @throws BooleanQuery.TooManyClauses
67 * @deprecated Hits will be removed in Lucene 3.0. Use
68 * {@link #search(Query, Filter, int, Sort)} instead.
69 */
70 public Hits search(Query query, Filter filter, Sort sort)
71 throws IOException {
72 return new Hits(this, query, filter, sort);
73 }
74
75 /** Search implementation with arbitrary sorting. Finds
76 * the top <code>n</code> hits for <code>query</code>, applying
77 * <code>filter</code> if non-null, and sorting the hits by the criteria in
78 * <code>sort</code>.
79 *
80 * <p>Applications should usually call {@link
81 * Searcher#search(Query,Filter,Sort)} instead.
82 * @throws BooleanQuery.TooManyClauses
83 */
84 public TopFieldDocs search(Query query, Filter filter, int n,
85 Sort sort) throws IOException {
86 return search(createWeight(query), filter, n, sort);
87 }
88
89 /** Lower-level search API.
90 *
91 * <p>{@link HitCollector#collect(int,float)} is called for every matching
92 * document.
93 *
94 * <p>Applications should only use this if they need <i>all</i> of the
95 * matching documents. The high-level search API ({@link
96 * Searcher#search(Query)}) is usually more efficient, as it skips
97 * non-high-scoring hits.
98 * <p>Note: The <code>score</code> passed to this method is a raw score.
99 * In other words, the score will not necessarily be a float whose value is
100 * between 0 and 1.
101 * @throws BooleanQuery.TooManyClauses
102 */
103 public void search(Query query, HitCollector results)
104 throws IOException {
105 search(query, (Filter)null, results);
106 }
107
108 /** Lower-level search API.
109 *
110 * <p>{@link HitCollector#collect(int,float)} is called for every matching
111 * document.
112 * <br>HitCollector-based access to remote indexes is discouraged.
113 *
114 * <p>Applications should only use this if they need <i>all</i> of the
115 * matching documents. The high-level search API ({@link
116 * Searcher#search(Query, Filter, int)}) is usually more efficient, as it skips
117 * non-high-scoring hits.
118 *
119 * @param query to match documents
120 * @param filter if non-null, used to permit documents to be collected.
121 * @param results to receive hits
122 * @throws BooleanQuery.TooManyClauses
123 */
124 public void search(Query query, Filter filter, HitCollector results)
125 throws IOException {
126 search(createWeight(query), filter, results);
127 }
128
129 /** Finds the top <code>n</code>
130 * hits for <code>query</code>, applying <code>filter</code> if non-null.
131 *
132 * @throws BooleanQuery.TooManyClauses
133 */
134 public TopDocs search(Query query, Filter filter, int n)
135 throws IOException {
136 return search(createWeight(query), filter, n);
137 }
138
139 /** Finds the top <code>n</code>
140 * hits for <code>query</code>.
141 *
142 * @throws BooleanQuery.TooManyClauses
143 */
144 public TopDocs search(Query query, int n)
145 throws IOException {
146 return search(query, null, n);
147 }
148
149 /** Returns an Explanation that describes how <code>doc</code> scored against
150 * <code>query</code>.
151 *
152 * <p>This is intended to be used in developing Similarity implementations,
153 * and, for good performance, should not be displayed with every hit.
154 * Computing an explanation is as expensive as executing the query over the
155 * entire index.
156 */
157 public Explanation explain(Query query, int doc) throws IOException {
158 return explain(createWeight(query), doc);
159 }
160
161 /** The Similarity implementation used by this searcher. */
162 private Similarity similarity = Similarity.getDefault();
163
164 /** Expert: Set the Similarity implementation used by this Searcher.
165 *
166 * @see Similarity#setDefault(Similarity)
167 */
168 public void setSimilarity(Similarity similarity) {
169 this.similarity = similarity;
170 }
171
172 /** Expert: Return the Similarity implementation used by this Searcher.
173 *
174 * <p>This defaults to the current value of {@link Similarity#getDefault()}.
175 */
176 public Similarity getSimilarity() {
177 return this.similarity;
178 }
179
180 /**
181 * creates a weight for <code>query</code>
182 * @return new weight
183 */
184 protected Weight createWeight(Query query) throws IOException {
185 return query.weight(this);
186 }
187
188 // inherit javadoc
189 public int[] docFreqs(Term[] terms) throws IOException {
190 int[] result = new int[terms.length];
191 for (int i = 0; i < terms.length; i++) {
192 result[i] = docFreq(terms[i]);
193 }
194 return result;
195 }
196
197 /* The following abstract methods were added as a workaround for GCJ bug #15411.
198 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15411
199 */
200 abstract public void search(Weight weight, Filter filter, HitCollector results) throws IOException;
201 abstract public void close() throws IOException;
202 abstract public int docFreq(Term term) throws IOException;
203 abstract public int maxDoc() throws IOException;
204 abstract public TopDocs search(Weight weight, Filter filter, int n) throws IOException;
205 abstract public Document doc(int i) throws CorruptIndexException, IOException;
206 abstract public Query rewrite(Query query) throws IOException;
207 abstract public Explanation explain(Weight weight, int doc) throws IOException;
208 abstract public TopFieldDocs search(Weight weight, Filter filter, int n, Sort sort) throws IOException;
209 /* End patch for GCJ bug #15411. */
210 }