Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Weight.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 
22 namespace Lucene.Net.Search
23 {
24 
25  /// <summary> Expert: Calculate query weights and build query scorers.
26  /// <p/>
27  /// The purpose of <see cref="Weight" /> is to ensure searching does not
28  /// modify a <see cref="Query" />, so that a <see cref="Query" /> instance can be reused. <br/>
29  /// <see cref="Searcher" /> dependent state of the query should reside in the
30  /// <see cref="Weight" />. <br/>
31  /// <see cref="IndexReader" /> dependent state should reside in the <see cref="Scorer" />.
32  /// <p/>
33  /// A <c>Weight</c> is used in the following way:
34  /// <list type="bullet">
35  /// <item>A <c>Weight</c> is constructed by a top-level query, given a
36  /// <c>Searcher</c> (<see cref="Lucene.Net.Search.Query.CreateWeight(Searcher)" />).</item>
37  /// <item>The <see cref="GetSumOfSquaredWeights()" /> method is called on the
38  /// <c>Weight</c> to compute the query normalization factor
39  /// <see cref="Similarity.QueryNorm(float)" /> of the query clauses contained in the
40  /// query.</item>
41  /// <item>The query normalization factor is passed to <see cref="Normalize(float)" />. At
42  /// this point the weighting is complete.</item>
43  /// <item>A <c>Scorer</c> is constructed by <see cref="Scorer(IndexReader,bool,bool)" />.</item>
44  /// </list>
45  ///
46  /// </summary>
47  /// <since> 2.9
48  /// </since>
49  [Serializable]
50  public abstract class Weight
51  {
52 
53  /// <summary> An explanation of the score computation for the named document.
54  ///
55  /// </summary>
56  /// <param name="reader">sub-reader containing the give doc
57  /// </param>
58  /// <param name="doc">
59  /// </param>
60  /// <returns> an Explanation for the score
61  /// </returns>
62  /// <throws> IOException </throws>
63  public abstract Explanation Explain(IndexReader reader, int doc);
64 
65  /// <summary>The query that this concerns. </summary>
66  public abstract Query Query { get; }
67 
68  /// <summary>The weight for this query. </summary>
69  public abstract float Value { get; }
70 
71  /// <summary>Assigns the query normalization factor to this. </summary>
72  public abstract void Normalize(float norm);
73 
74  /// <summary> Returns a <see cref="Scorer" /> which scores documents in/out-of order according
75  /// to <c>scoreDocsInOrder</c>.
76  /// <p/>
77  /// <b>NOTE:</b> even if <c>scoreDocsInOrder</c> is false, it is
78  /// recommended to check whether the returned <c>Scorer</c> indeed scores
79  /// documents out of order (i.e., call <see cref="GetScoresDocsOutOfOrder()" />), as
80  /// some <c>Scorer</c> implementations will always return documents
81  /// in-order.<br/>
82  /// <b>NOTE:</b> null can be returned if no documents will be scored by this
83  /// query.
84  ///
85  /// </summary>
86  /// <param name="reader">
87  /// the <see cref="IndexReader" /> for which to return the <see cref="Lucene.Net.Search.Scorer" />.
88  /// </param>
89  /// <param name="scoreDocsInOrder">specifies whether in-order scoring of documents is required. Note
90  /// that if set to false (i.e., out-of-order scoring is required),
91  /// this method can return whatever scoring mode it supports, as every
92  /// in-order scorer is also an out-of-order one. However, an
93  /// out-of-order scorer may not support <see cref="DocIdSetIterator.NextDoc" />
94  /// and/or <see cref="DocIdSetIterator.Advance(int)" />, therefore it is recommended to
95  /// request an in-order scorer if use of these methods is required.
96  /// </param>
97  /// <param name="topScorer">
98  /// if true, <see cref="Lucene.Net.Search.Scorer.Score(Lucene.Net.Search.Collector)" /> will be called; if false,
99  /// <see cref="DocIdSetIterator.NextDoc" /> and/or <see cref="DocIdSetIterator.Advance(int)" /> will
100  /// be called.
101  /// </param>
102  /// <returns> a <see cref="Scorer" /> which scores documents in/out-of order.
103  /// </returns>
104  /// <throws> IOException </throws>
105  public abstract Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer);
106 
107  /// <summary>The sum of squared weights of contained query clauses. </summary>
108  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
109  public abstract float GetSumOfSquaredWeights();
110 
111  /// <summary> Returns true iff this implementation scores docs only out of order. This
112  /// method is used in conjunction with <see cref="Collector" />'s
113  /// <see cref="Collector.AcceptsDocsOutOfOrder()">AcceptsDocsOutOfOrder</see> and
114  /// <see cref="Scorer(Lucene.Net.Index.IndexReader, bool, bool)" /> to
115  /// create a matching <see cref="Scorer" /> instance for a given <see cref="Collector" />, or
116  /// vice versa.
117  /// <p/>
118  /// <b>NOTE:</b> the default implementation returns <c>false</c>, i.e.
119  /// the <c>Scorer</c> scores documents in-order.
120  /// </summary>
121  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
122  public virtual bool GetScoresDocsOutOfOrder()
123  {
124  return false;
125  }
126  }
127 }