1 package org.apache.lucene.search;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
21 /** A Scorer for queries with a required part and an optional part.
22 * Delays skipTo() on the optional part until a score() is needed.
24 * This <code>Scorer</code> implements {@link Scorer#skipTo(int)}.
26 class ReqOptSumScorer extends Scorer {
27 /** 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 /** Construct a <code>ReqOptScorer</code>.
34 * @param reqScorer The required scorer. This must match.
35 * @param optScorer The optional scorer. This is used for scoring only.
37 public ReqOptSumScorer(
41 super(reqScorer.weight);
42 this.reqScorer = reqScorer;
43 this.optScorer = optScorer;
47 public int nextDoc() throws IOException {
48 return reqScorer.nextDoc();
52 public int advance(int target) throws IOException {
53 return reqScorer.advance(target);
58 return reqScorer.docID();
61 /** Returns the score of the current document matching the query.
62 * Initially invalid, until {@link #nextDoc()} is called the first time.
63 * @return The score of the required scorer, eventually increased by the score
64 * of the optional scorer when it also matches the current document.
67 public float score() throws IOException {
68 int curDoc = reqScorer.docID();
69 float reqScore = reqScorer.score();
70 if (optScorer == null) {
74 int optScorerDoc = optScorer.docID();
75 if (optScorerDoc < curDoc && (optScorerDoc = optScorer.advance(curDoc)) == NO_MORE_DOCS) {
80 return optScorerDoc == curDoc ? reqScore + optScorer.score() : reqScore;