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;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.util.LuceneTestCase;
25 public class TestScoreCachingWrappingScorer extends LuceneTestCase {
27 private static final class SimpleScorer extends Scorer {
31 public SimpleScorer() {
32 super((Similarity)null);
35 @Override public float score() throws IOException {
36 // advance idx on purpose, so that consecutive calls to score will get
37 // different results. This is to emulate computation of a score. If
38 // ScoreCachingWrappingScorer is used, this should not be called more than
40 return idx == scores.length ? Float.NaN : scores[idx++];
43 @Override public int docID() { return doc; }
45 @Override public int nextDoc() throws IOException {
46 return ++doc < scores.length ? doc : NO_MORE_DOCS;
49 @Override public int advance(int target) throws IOException {
51 return doc < scores.length ? doc : NO_MORE_DOCS;
56 private static final class ScoreCachingCollector extends Collector {
59 private Scorer scorer;
62 public ScoreCachingCollector(int numToCollect) {
63 mscores = new float[numToCollect];
66 @Override public void collect(int doc) throws IOException {
67 // just a sanity check to avoid IOOB.
68 if (idx == mscores.length) {
72 // just call score() a couple of times and record the score.
73 mscores[idx] = scorer.score();
74 mscores[idx] = scorer.score();
75 mscores[idx] = scorer.score();
79 @Override public void setNextReader(IndexReader reader, int docBase)
83 @Override public void setScorer(Scorer scorer) throws IOException {
84 this.scorer = new ScoreCachingWrappingScorer(scorer);
87 @Override public boolean acceptsDocsOutOfOrder() {
93 private static final float[] scores = new float[] { 0.7767749f, 1.7839992f,
94 8.9925785f, 7.9608946f, 0.07948637f, 2.6356435f, 7.4950366f, 7.1490803f,
95 8.108544f, 4.961808f, 2.2423935f, 7.285586f, 4.6699767f };
97 public void testGetScores() throws Exception {
99 Scorer s = new SimpleScorer();
100 ScoreCachingCollector scc = new ScoreCachingCollector(scores.length);
103 // We need to iterate on the scorer so that its doc() advances.
105 while ((doc = s.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
109 for (int i = 0; i < scores.length; i++) {
110 assertEquals(scores[i], scc.mscores[i], 0f);