1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
23 * A {@link Scorer} which wraps another scorer and caches the score of the
24 * current document. Successive calls to {@link #score()} will return the same
25 * result and will not invoke the wrapped Scorer's score() method, unless the
26 * current document has changed.<br>
27 * This class might be useful due to the changes done to the {@link Collector}
28 * interface, in which the score is not computed for a document by default, only
29 * if the collector requests it. Some collectors may need to use the score in
30 * several places, however all they have in hand is a {@link Scorer} object, and
31 * might end up computing the score of a document more than once.
33 public class ScoreCachingWrappingScorer extends Scorer {
35 private final Scorer scorer;
36 private int curDoc = -1;
37 private float curScore;
39 /** Creates a new instance by wrapping the given scorer. */
40 public ScoreCachingWrappingScorer(Scorer scorer) {
41 super(scorer.getSimilarity(), scorer.weight);
46 protected boolean score(Collector collector, int max, int firstDocID) throws IOException {
47 return scorer.score(collector, max, firstDocID);
51 public Similarity getSimilarity() {
52 return scorer.getSimilarity();
56 public float score() throws IOException {
57 int doc = scorer.docID();
59 curScore = scorer.score();
68 return scorer.docID();
72 public int nextDoc() throws IOException {
73 return scorer.nextDoc();
77 public void score(Collector collector) throws IOException {
78 scorer.score(collector);
82 public int advance(int target) throws IOException {
83 return scorer.advance(target);