2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Search / ReqOptSumScorer.cs
blobf2111f87b965263068d7707136c20d95eebafaa8
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.
16 using System;
17 namespace Lucene.Net.Search
20 /// <summary>A Scorer for queries with a required part and an optional part.
21 /// Delays SkipTo() on the optional part until a score() is needed.
22 /// <br>
23 /// This <code>Scorer</code> implements {@link Scorer#SkipTo(int)}.
24 /// </summary>
25 public class ReqOptSumScorer : Scorer
27 /// <summary>The scorers passed from the constructor.
28 /// These are set to null as soon as their Next() or SkipTo() returns false.
29 /// </summary>
30 private Scorer reqScorer;
31 private Scorer optScorer;
33 /// <summary>Construct a <code>ReqOptScorer</code>.</summary>
34 /// <param name="reqScorer">The required scorer. This must match.
35 /// </param>
36 /// <param name="optScorer">The optional scorer. This is used for scoring only.
37 /// </param>
38 public ReqOptSumScorer(Scorer reqScorer, Scorer optScorer) : base(null)
39 { // No similarity used.
40 this.reqScorer = reqScorer;
41 this.optScorer = optScorer;
44 private bool firstTimeOptScorer = true;
46 public override bool Next()
48 return reqScorer.Next();
51 public override bool SkipTo(int target)
53 return reqScorer.SkipTo(target);
56 public override int Doc()
58 return reqScorer.Doc();
61 /// <summary>Returns the score of the current document matching the query.
62 /// Initially invalid, until {@link #Next()} is called the first time.
63 /// </summary>
64 /// <returns> The score of the required scorer, eventually increased by the score
65 /// of the optional scorer when it also matches the current document.
66 /// </returns>
67 public override float Score()
69 int curDoc = reqScorer.Doc();
70 float reqScore = reqScorer.Score();
71 if (firstTimeOptScorer)
73 firstTimeOptScorer = false;
74 if (!optScorer.SkipTo(curDoc))
76 optScorer = null;
77 return reqScore;
80 else if (optScorer == null)
82 return reqScore;
84 else if ((optScorer.Doc() < curDoc) && (!optScorer.SkipTo(curDoc)))
86 optScorer = null;
87 return reqScore;
89 // assert (optScorer != null) && (optScorer.doc() >= curDoc);
90 return (optScorer.Doc() == curDoc)?reqScore + optScorer.Score():reqScore;
93 /// <summary>Explain the score of a document.</summary>
94 /// <todo> Also show the total score. </todo>
95 /// <summary> See BooleanScorer.explain() on how to do this.
96 /// </summary>
97 public override Explanation Explain(int doc)
99 Explanation res = new Explanation();
100 res.SetDescription("required, optional");
101 res.AddDetail(reqScorer.Explain(doc));
102 res.AddDetail(optScorer.Explain(doc));
103 return res;