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
CustomScoreProvider.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 using System.Collections.Generic;
20 using System.Text;
21 
22 using Lucene.Net.Index;
23 
24 namespace Lucene.Net.Search.Function
25 {
26  /// <summary>
27  /// An instance of this subclass should be returned by
28  /// <see cref="CustomScoreQuery.GetCustomScoreProvider" />, if you want
29  /// to modify the custom score calculation of a <see cref="CustomScoreQuery" />.
30  /// <para>Since Lucene 2.9, queries operate on each segment of an Index separately,
31  /// so overriding the similar (now deprecated) methods in <see cref="CustomScoreQuery" />
32  /// is no longer suitable, as the supplied <c>doc</c> ID is per-segment
33  /// and without knowledge of the IndexReader you cannot access the
34  /// document or <see cref="FieldCache" />.</para>
35  ///
36  /// @lucene.experimental
37  /// @since 2.9.2
38  /// </summary>
39  public class CustomScoreProvider
40  {
41 
42  protected IndexReader reader;
43 
44  /// <summary>
45  /// Creates a new instance of the provider class for the given IndexReader.
46  /// </summary>
48  {
49  this.reader = reader;
50  }
51 
52  /// <summary>
53  /// * Compute a custom score by the subQuery score and a number of
54  /// ValueSourceQuery scores.
55  /// <p/>
56  /// Subclasses can override this method to modify the custom score.
57  /// <p/>
58  /// If your custom scoring is different than the default herein you
59  /// should override at least one of the two customScore() methods.
60  /// If the number of ValueSourceQueries is always &lt; 2 it is
61  /// sufficient to override the other
62  /// <see cref="CustomScore(int, float, float)">CustomScore()</see>
63  /// method, which is simpler.
64  /// <p/>
65  /// The default computation herein is a multiplication of given scores:
66  /// <pre>
67  /// ModifiedScore = valSrcScore * valSrcScores[0] * valSrcScores[1] * ...
68  /// </pre>
69  /// </summary>
70  /// <param name="doc">id of scored doc</param>
71  /// <param name="subQueryScore">score of that doc by the subQuery</param>
72  /// <param name="valSrcScores">scores of that doc by the ValueSourceQuery</param>
73  /// <returns>custom score</returns>
74  public virtual float CustomScore(int doc, float subQueryScore, float[] valSrcScores)
75  {
76  if (valSrcScores.Length == 1)
77  {
78  return CustomScore(doc, subQueryScore, valSrcScores[0]);
79  }
80  if (valSrcScores.Length == 0)
81  {
82  return CustomScore(doc, subQueryScore, 1);
83  }
84  float score = subQueryScore;
85  for (int i = 0; i < valSrcScores.Length; i++)
86  {
87  score *= valSrcScores[i];
88  }
89  return score;
90  }
91 
92  /// <summary>
93  /// Compute a custom score by the subQuery score and the ValueSourceQuery score.
94  /// <p/>
95  /// Subclasses can override this method to modify the custom score.
96  /// <p/>
97  /// If your custom scoring is different than the default herein you
98  /// should override at least one of the two customScore() methods.
99  /// If the number of ValueSourceQueries is always &lt; 2 it is
100  /// sufficient to override this customScore() method, which is simpler.
101  /// <p/>
102  /// The default computation herein is a multiplication of the two scores:
103  /// <pre>
104  /// ModifiedScore = subQueryScore * valSrcScore
105  /// </pre>
106  /// </summary>
107  /// <param name="doc">id of scored doc</param>
108  /// <param name="subQueryScore">score of that doc by the subQuery</param>
109  /// <param name="valSrcScore">score of that doc by the ValueSourceQuery</param>
110  /// <returns>custom score</returns>
111  public virtual float CustomScore(int doc, float subQueryScore, float valSrcScore)
112  {
113  return subQueryScore * valSrcScore;
114  }
115 
116  /// <summary>
117  /// Explain the custom score.
118  /// Whenever overriding <see cref="CustomScore(int, float, float[])" />,
119  /// this method should also be overridden to provide the correct explanation
120  /// for the part of the custom scoring.
121  /// </summary>
122  /// <param name="doc">doc being explained</param>
123  /// <param name="subQueryExpl">explanation for the sub-query part</param>
124  /// <param name="valSrcExpls">explanation for the value source part</param>
125  /// <returns>an explanation for the custom score</returns>
126  public virtual Explanation CustomExplain(int doc, Explanation subQueryExpl, Explanation[] valSrcExpls)
127  {
128  if (valSrcExpls.Length == 1)
129  {
130  return CustomExplain(doc, subQueryExpl, valSrcExpls[0]);
131  }
132  if (valSrcExpls.Length == 0)
133  {
134  return subQueryExpl;
135  }
136  float valSrcScore = 1;
137  for (int i = 0; i < valSrcExpls.Length; i++)
138  {
139  valSrcScore *= valSrcExpls[i].Value;
140  }
141  Explanation exp = new Explanation(valSrcScore * subQueryExpl.Value, "custom score: product of:");
142  exp.AddDetail(subQueryExpl);
143  for (int i = 0; i < valSrcExpls.Length; i++)
144  {
145  exp.AddDetail(valSrcExpls[i]);
146  }
147  return exp;
148  }
149 
150  /// <summary>
151  /// Explain the custom score.
152  /// Whenever overriding <see cref="CustomScore(int, float, float)" />,
153  /// this method should also be overridden to provide the correct explanation
154  /// for the part of the custom scoring.
155  ///
156  /// </summary>
157  /// <param name="doc">doc being explained</param>
158  /// <param name="subQueryExpl">explanation for the sub-query part</param>
159  /// <param name="valSrcExpl">explanation for the value source part</param>
160  /// <returns>an explanation for the custom score</returns>
161  public virtual Explanation CustomExplain(int doc, Explanation subQueryExpl, Explanation valSrcExpl)
162  {
163  float valSrcScore = 1;
164  if (valSrcExpl != null)
165  {
166  valSrcScore *= valSrcExpl.Value;
167  }
168  Explanation exp = new Explanation(valSrcScore * subQueryExpl.Value, "custom score: product of:");
169  exp.AddDetail(subQueryExpl);
170  exp.AddDetail(valSrcExpl);
171  return exp;
172  }
173 
174  }
175 }