add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / Weight.java
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 import java.io.Serializable;
22
23 import org.apache.lucene.index.IndexReader;
24
25 /**
26  * Expert: Calculate query weights and build query scorers.
27  * <p>
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}.
33  * <p>
34  * A <code>Weight</code> is used in the following way:
35  * <ol>
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
41  * query.
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)}.
45  * </ol>
46  * 
47  * @since 2.9
48  */
49 public abstract class Weight implements Serializable {
50
51   /**
52    * An explanation of the score computation for the named document.
53    * 
54    * @param reader sub-reader containing the give doc
55    * @param doc
56    * @return an Explanation for the score
57    * @throws IOException
58    */
59   public abstract Explanation explain(IndexReader reader, int doc) throws IOException;
60
61   /** The query that this concerns. */
62   public abstract Query getQuery();
63
64   /** The weight for this query. */
65   public abstract float getValue();
66
67   /** Assigns the query normalization factor to this. */
68   public abstract void normalize(float norm);
69
70   /**
71    * Returns a {@link Scorer} which scores documents in/out-of order according
72    * to <code>scoreDocsInOrder</code>.
73    * <p>
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
78    * in-order.<br>
79    * <b>NOTE:</b> null can be returned if no documents will be scored by this
80    * query.
81    * 
82    * @param reader
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.
92    * @param topScorer
93    *          if true, {@link Scorer#score(Collector)} will be called; if false,
94    *          {@link Scorer#nextDoc()} and/or {@link Scorer#advance(int)} will
95    *          be called.
96    * @return a {@link Scorer} which scores documents in/out-of order.
97    * @throws IOException
98    */
99   public abstract Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
100       boolean topScorer) throws IOException;
101   
102   /** The sum of squared weights of contained query clauses. */
103   public abstract float sumOfSquaredWeights() throws IOException;
104
105   /**
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
111    * vice versa.
112    * <p>
113    * <b>NOTE:</b> the default implementation returns <code>false</code>, i.e.
114    * the <code>Scorer</code> scores documents in-order.
115    */
116   public boolean scoresDocsOutOfOrder() { return false; }
117
118 }