01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21:
22: /** 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: */
27: public class ReqOptSumScorer extends Scorer {
28: /** The scorers passed from the constructor.
29: * These are set to null as soon as their next() or skipTo() returns false.
30: */
31: private Scorer reqScorer;
32: private Scorer optScorer;
33:
34: /** Construct a <code>ReqOptScorer</code>.
35: * @param reqScorer The required scorer. This must match.
36: * @param optScorer The optional scorer. This is used for scoring only.
37: */
38: public ReqOptSumScorer(Scorer reqScorer, Scorer optScorer) {
39: super (null); // No similarity used.
40: this .reqScorer = reqScorer;
41: this .optScorer = optScorer;
42: }
43:
44: private boolean firstTimeOptScorer = true;
45:
46: public boolean next() throws IOException {
47: return reqScorer.next();
48: }
49:
50: public boolean skipTo(int target) throws IOException {
51: return reqScorer.skipTo(target);
52: }
53:
54: public int doc() {
55: return reqScorer.doc();
56: }
57:
58: /** Returns the score of the current document matching the query.
59: * Initially invalid, until {@link #next()} is called the first time.
60: * @return The score of the required scorer, eventually increased by the score
61: * of the optional scorer when it also matches the current document.
62: */
63: public float score() throws IOException {
64: int curDoc = reqScorer.doc();
65: float reqScore = reqScorer.score();
66: if (firstTimeOptScorer) {
67: firstTimeOptScorer = false;
68: if (!optScorer.skipTo(curDoc)) {
69: optScorer = null;
70: return reqScore;
71: }
72: } else if (optScorer == null) {
73: return reqScore;
74: } else if ((optScorer.doc() < curDoc)
75: && (!optScorer.skipTo(curDoc))) {
76: optScorer = null;
77: return reqScore;
78: }
79: // assert (optScorer != null) && (optScorer.doc() >= curDoc);
80: return (optScorer.doc() == curDoc) ? reqScore
81: + optScorer.score() : reqScore;
82: }
83:
84: /** Explain the score of a document.
85: * @todo Also show the total score.
86: * See BooleanScorer.explain() on how to do this.
87: */
88: public Explanation explain(int doc) throws IOException {
89: Explanation res = new Explanation();
90: res.setDescription("required, optional");
91: res.addDetail(reqScorer.explain(doc));
92: res.addDetail(optScorer.explain(doc));
93: return res;
94: }
95: }
|