001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021:
022: import org.apache.lucene.index.CorruptIndexException;
023: import org.apache.lucene.index.Term;
024: import org.apache.lucene.document.Document;
025:
026: /** An abstract base class for search implementations.
027: * Implements the main search methods.
028: *
029: * <p>Note that you can only access Hits from a Searcher as long as it is
030: * not yet closed, otherwise an IOException will be thrown.
031: */
032: public abstract class Searcher implements Searchable {
033:
034: /** Returns the documents matching <code>query</code>.
035: * @throws BooleanQuery.TooManyClauses
036: */
037: public final Hits search(Query query) throws IOException {
038: return search(query, (Filter) null);
039: }
040:
041: /** Returns the documents matching <code>query</code> and
042: * <code>filter</code>.
043: * @throws BooleanQuery.TooManyClauses
044: */
045: public Hits search(Query query, Filter filter) throws IOException {
046: return new Hits(this , query, filter);
047: }
048:
049: /** Returns documents matching <code>query</code> sorted by
050: * <code>sort</code>.
051: * @throws BooleanQuery.TooManyClauses
052: */
053: public Hits search(Query query, Sort sort) throws IOException {
054: return new Hits(this , query, null, sort);
055: }
056:
057: /** Returns documents matching <code>query</code> and <code>filter</code>,
058: * sorted by <code>sort</code>.
059: * @throws BooleanQuery.TooManyClauses
060: */
061: public Hits search(Query query, Filter filter, Sort sort)
062: throws IOException {
063: return new Hits(this , query, filter, sort);
064: }
065:
066: /** Expert: Low-level search implementation with arbitrary sorting. Finds
067: * the top <code>n</code> hits for <code>query</code>, applying
068: * <code>filter</code> if non-null, and sorting the hits by the criteria in
069: * <code>sort</code>.
070: *
071: * <p>Applications should usually call {@link
072: * Searcher#search(Query,Filter,Sort)} instead.
073: * @throws BooleanQuery.TooManyClauses
074: */
075: public TopFieldDocs search(Query query, Filter filter, int n,
076: Sort sort) throws IOException {
077: return search(createWeight(query), filter, n, sort);
078: }
079:
080: /** Lower-level search API.
081: *
082: * <p>{@link HitCollector#collect(int,float)} is called for every non-zero
083: * scoring document.
084: *
085: * <p>Applications should only use this if they need <i>all</i> of the
086: * matching documents. The high-level search API ({@link
087: * Searcher#search(Query)}) is usually more efficient, as it skips
088: * non-high-scoring hits.
089: * <p>Note: The <code>score</code> passed to this method is a raw score.
090: * In other words, the score will not necessarily be a float whose value is
091: * between 0 and 1.
092: * @throws BooleanQuery.TooManyClauses
093: */
094: public void search(Query query, HitCollector results)
095: throws IOException {
096: search(query, (Filter) null, results);
097: }
098:
099: /** Lower-level search API.
100: *
101: * <p>{@link HitCollector#collect(int,float)} is called for every non-zero
102: * scoring document.
103: * <br>HitCollector-based access to remote indexes is discouraged.
104: *
105: * <p>Applications should only use this if they need <i>all</i> of the
106: * matching documents. The high-level search API ({@link
107: * Searcher#search(Query)}) is usually more efficient, as it skips
108: * non-high-scoring hits.
109: *
110: * @param query to match documents
111: * @param filter if non-null, a bitset used to eliminate some documents
112: * @param results to receive hits
113: * @throws BooleanQuery.TooManyClauses
114: */
115: public void search(Query query, Filter filter, HitCollector results)
116: throws IOException {
117: search(createWeight(query), filter, results);
118: }
119:
120: /** Expert: Low-level search implementation. Finds the top <code>n</code>
121: * hits for <code>query</code>, applying <code>filter</code> if non-null.
122: *
123: * <p>Called by {@link Hits}.
124: *
125: * <p>Applications should usually call {@link Searcher#search(Query)} or
126: * {@link Searcher#search(Query,Filter)} instead.
127: * @throws BooleanQuery.TooManyClauses
128: */
129: public TopDocs search(Query query, Filter filter, int n)
130: throws IOException {
131: return search(createWeight(query), filter, n);
132: }
133:
134: /** Returns an Explanation that describes how <code>doc</code> scored against
135: * <code>query</code>.
136: *
137: * <p>This is intended to be used in developing Similarity implementations,
138: * and, for good performance, should not be displayed with every hit.
139: * Computing an explanation is as expensive as executing the query over the
140: * entire index.
141: */
142: public Explanation explain(Query query, int doc) throws IOException {
143: return explain(createWeight(query), doc);
144: }
145:
146: /** The Similarity implementation used by this searcher. */
147: private Similarity similarity = Similarity.getDefault();
148:
149: /** Expert: Set the Similarity implementation used by this Searcher.
150: *
151: * @see Similarity#setDefault(Similarity)
152: */
153: public void setSimilarity(Similarity similarity) {
154: this .similarity = similarity;
155: }
156:
157: /** Expert: Return the Similarity implementation used by this Searcher.
158: *
159: * <p>This defaults to the current value of {@link Similarity#getDefault()}.
160: */
161: public Similarity getSimilarity() {
162: return this .similarity;
163: }
164:
165: /**
166: * creates a weight for <code>query</code>
167: * @return new weight
168: */
169: protected Weight createWeight(Query query) throws IOException {
170: return query.weight(this );
171: }
172:
173: // inherit javadoc
174: public int[] docFreqs(Term[] terms) throws IOException {
175: int[] result = new int[terms.length];
176: for (int i = 0; i < terms.length; i++) {
177: result[i] = docFreq(terms[i]);
178: }
179: return result;
180: }
181:
182: /* The following abstract methods were added as a workaround for GCJ bug #15411.
183: * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15411
184: */
185: abstract public void search(Weight weight, Filter filter,
186: HitCollector results) throws IOException;
187:
188: abstract public void close() throws IOException;
189:
190: abstract public int docFreq(Term term) throws IOException;
191:
192: abstract public int maxDoc() throws IOException;
193:
194: abstract public TopDocs search(Weight weight, Filter filter, int n)
195: throws IOException;
196:
197: abstract public Document doc(int i) throws CorruptIndexException,
198: IOException;
199:
200: abstract public Query rewrite(Query query) throws IOException;
201:
202: abstract public Explanation explain(Weight weight, int doc)
203: throws IOException;
204:
205: abstract public TopFieldDocs search(Weight weight, Filter filter,
206: int n, Sort sort) throws IOException;
207: /* End patch for GCJ bug #15411. */
208: }
|