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 /**
23 * Expert: Common scoring functionality for different types of queries.
24 *
25 * <p>
26 * A <code>Scorer</code> either iterates over documents matching a
27 * query in increasing order of doc Id, or provides an explanation of
28 * the score for a query for a given document.
29 * </p>
30 * <p>
31 * Document scores are computed using a given <code>Similarity</code>
32 * implementation.
33 * </p>
34 * @see BooleanQuery#setAllowDocsOutOfOrder
35 */
36 public abstract class Scorer extends DocIdSetIterator {
37 private Similarity similarity;
38
39 /** Constructs a Scorer.
40 * @param similarity The <code>Similarity</code> implementation used by this scorer.
41 */
42 protected Scorer(Similarity similarity) {
43 this.similarity = similarity;
44 }
45
46 /** Returns the Similarity implementation used by this scorer. */
47 public Similarity getSimilarity() {
48 return this.similarity;
49 }
50
51 /** Scores and collects all matching documents.
52 * @param hc The collector to which all matching documents are passed through
53 * {@link HitCollector#collect(int, float)}.
54 * <br>When this method is used the {@link #explain(int)} method should not be used.
55 */
56 public void score(HitCollector hc) throws IOException {
57 while (next()) {
58 hc.collect(doc(), score());
59 }
60 }
61
62 /** Expert: Collects matching documents in a range. Hook for optimization.
63 * Note that {@link #next()} must be called once before this method is called
64 * for the first time.
65 * @param hc The collector to which all matching documents are passed through
66 * {@link HitCollector#collect(int, float)}.
67 * @param max Do not score documents past this.
68 * @return true if more matching documents may remain.
69 */
70 protected boolean score(HitCollector hc, int max) throws IOException {
71 while (doc() < max) {
72 hc.collect(doc(), score());
73 if (!next())
74 return false;
75 }
76 return true;
77 }
78
79 /** Returns the score of the current document matching the query.
80 * Initially invalid, until {@link #next()} or {@link #skipTo(int)}
81 * is called the first time.
82 */
83 public abstract float score() throws IOException;
84
85 /** Returns an explanation of the score for a document.
86 * <br>When this method is used, the {@link #next()}, {@link #skipTo(int)} and
87 * {@link #score(HitCollector)} methods should not be used.
88 * @param doc The document number for the explanation.
89 */
90 public abstract Explanation explain(int doc) throws IOException;
91
92 }