Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 {
39  public class CustomScoreProvider
40  {
41 
42  protected IndexReader reader;
43 
48  {
49  this.reader = reader;
50  }
51 
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 
111  public virtual float CustomScore(int doc, float subQueryScore, float valSrcScore)
112  {
113  return subQueryScore * valSrcScore;
114  }
115 
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 
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 }