1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.io.Serializable;
23 import org.apache.lucene.index.IndexReader;
26 * Expert: Calculate query weights and build query scorers.
28 * The purpose of {@link Weight} is to ensure searching does not
29 * modify a {@link Query}, so that a {@link Query} instance can be reused. <br>
30 * {@link Searcher} dependent state of the query should reside in the
31 * {@link Weight}. <br>
32 * {@link IndexReader} dependent state should reside in the {@link Scorer}.
34 * A <code>Weight</code> is used in the following way:
36 * <li>A <code>Weight</code> is constructed by a top-level query, given a
37 * <code>Searcher</code> ({@link Query#createWeight(Searcher)}).
38 * <li>The {@link #sumOfSquaredWeights()} method is called on the
39 * <code>Weight</code> to compute the query normalization factor
40 * {@link Similarity#queryNorm(float)} of the query clauses contained in the
42 * <li>The query normalization factor is passed to {@link #normalize(float)}. At
43 * this point the weighting is complete.
44 * <li>A <code>Scorer</code> is constructed by {@link #scorer(IndexReader,boolean,boolean)}.
49 public abstract class Weight implements Serializable {
52 * An explanation of the score computation for the named document.
54 * @param reader sub-reader containing the give doc
56 * @return an Explanation for the score
59 public abstract Explanation explain(IndexReader reader, int doc) throws IOException;
61 /** The query that this concerns. */
62 public abstract Query getQuery();
64 /** The weight for this query. */
65 public abstract float getValue();
67 /** Assigns the query normalization factor to this. */
68 public abstract void normalize(float norm);
71 * Returns a {@link Scorer} which scores documents in/out-of order according
72 * to <code>scoreDocsInOrder</code>.
74 * <b>NOTE:</b> even if <code>scoreDocsInOrder</code> is false, it is
75 * recommended to check whether the returned <code>Scorer</code> indeed scores
76 * documents out of order (i.e., call {@link #scoresDocsOutOfOrder()}), as
77 * some <code>Scorer</code> implementations will always return documents
79 * <b>NOTE:</b> null can be returned if no documents will be scored by this
83 * the {@link IndexReader} for which to return the {@link Scorer}.
84 * @param scoreDocsInOrder
85 * specifies whether in-order scoring of documents is required. Note
86 * that if set to false (i.e., out-of-order scoring is required),
87 * this method can return whatever scoring mode it supports, as every
88 * in-order scorer is also an out-of-order one. However, an
89 * out-of-order scorer may not support {@link Scorer#nextDoc()}
90 * and/or {@link Scorer#advance(int)}, therefore it is recommended to
91 * request an in-order scorer if use of these methods is required.
93 * if true, {@link Scorer#score(Collector)} will be called; if false,
94 * {@link Scorer#nextDoc()} and/or {@link Scorer#advance(int)} will
96 * @return a {@link Scorer} which scores documents in/out-of order.
99 public abstract Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
100 boolean topScorer) throws IOException;
102 /** The sum of squared weights of contained query clauses. */
103 public abstract float sumOfSquaredWeights() throws IOException;
106 * Returns true iff this implementation scores docs only out of order. This
107 * method is used in conjunction with {@link Collector}'s
108 * {@link Collector#acceptsDocsOutOfOrder() acceptsDocsOutOfOrder} and
109 * {@link #scorer(org.apache.lucene.index.IndexReader, boolean, boolean)} to
110 * create a matching {@link Scorer} instance for a given {@link Collector}, or
113 * <b>NOTE:</b> the default implementation returns <code>false</code>, i.e.
114 * the <code>Scorer</code> scores documents in-order.
116 public boolean scoresDocsOutOfOrder() { return false; }