2 * Copyright 2005 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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.
23 /// This <code>Scorer</code> implements {@link Scorer#SkipTo(int)}.
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.
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.
36 /// <param name="optScorer">The optional scorer. This is used for scoring only.
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.
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.
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
))
80 else if (optScorer
== null)
84 else if ((optScorer
.Doc() < curDoc
) && (!optScorer
.SkipTo(curDoc
)))
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.
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
));