cvsimport
[beagle.git] / beagled / Lucene.Net / Search / ReqOptSumScorer.cs
blob8ce62c2c31b1e73a55fb40a7ddc049ac1bbfe7e2
1 /*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.Search
22 /// <summary>A Scorer for queries with a required part and an optional part.
23 /// Delays skipTo() on the optional part until a score() is needed.
24 /// <br>
25 /// This <code>Scorer</code> implements {@link Scorer#SkipTo(int)}.
26 /// </summary>
27 public class ReqOptSumScorer : Scorer
29 /// <summary>The scorers passed from the constructor.
30 /// These are set to null as soon as their next() or skipTo() returns false.
31 /// </summary>
32 private Scorer reqScorer;
33 private Scorer optScorer;
35 /// <summary>Construct a <code>ReqOptScorer</code>.</summary>
36 /// <param name="reqScorer">The required scorer. This must match.
37 /// </param>
38 /// <param name="optScorer">The optional scorer. This is used for scoring only.
39 /// </param>
40 public ReqOptSumScorer(Scorer reqScorer, Scorer optScorer) : base(null)
41 { // No similarity used.
42 this.reqScorer = reqScorer;
43 this.optScorer = optScorer;
46 private bool firstTimeOptScorer = true;
48 public override bool Next()
50 return reqScorer.Next();
53 public override bool SkipTo(int target)
55 return reqScorer.SkipTo(target);
58 public override int Doc()
60 return reqScorer.Doc();
63 /// <summary>Returns the score of the current document matching the query.
64 /// Initially invalid, until {@link #Next()} is called the first time.
65 /// </summary>
66 /// <returns> The score of the required scorer, eventually increased by the score
67 /// of the optional scorer when it also matches the current document.
68 /// </returns>
69 public override float Score()
71 int curDoc = reqScorer.Doc();
72 float reqScore = reqScorer.Score();
73 if (firstTimeOptScorer)
75 firstTimeOptScorer = false;
76 if (!optScorer.SkipTo(curDoc))
78 optScorer = null;
79 return reqScore;
82 else if (optScorer == null)
84 return reqScore;
86 else if ((optScorer.Doc() < curDoc) && (!optScorer.SkipTo(curDoc)))
88 optScorer = null;
89 return reqScore;
91 // assert (optScorer != null) && (optScorer.doc() >= curDoc);
92 return (optScorer.Doc() == curDoc)?reqScore + optScorer.Score():reqScore;
95 /// <summary>Explain the score of a document.</summary>
96 /// <todo> Also show the total score. </todo>
97 /// <summary> See BooleanScorer.explain() on how to do this.
98 /// </summary>
99 public override Explanation Explain(int doc)
101 Explanation res = new Explanation();
102 res.SetDescription("required, optional");
103 res.AddDetail(reqScorer.Explain(doc));
104 res.AddDetail(optScorer.Explain(doc));
105 return res;