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.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
28 public class TestPositiveScoresOnlyCollector extends LuceneTestCase {
30 private static final class SimpleScorer extends Scorer {
33 public SimpleScorer(Weight weight) {
37 @Override public float score() throws IOException {
38 return idx == scores.length ? Float.NaN : scores[idx];
41 @Override public int docID() { return idx; }
43 @Override public int nextDoc() throws IOException {
44 return ++idx != scores.length ? idx : NO_MORE_DOCS;
47 @Override public int advance(int target) throws IOException {
49 return idx < scores.length ? idx : NO_MORE_DOCS;
53 // The scores must have positive as well as negative values
54 private static final float[] scores = new float[] { 0.7767749f, -1.7839992f,
55 8.9925785f, 7.9608946f, -0.07948637f, 2.6356435f, 7.4950366f, 7.1490803f,
56 -8.108544f, 4.961808f, 2.2423935f, -7.285586f, 4.6699767f };
58 public void testNegativeScores() throws Exception {
60 // The Top*Collectors previously filtered out documents with <= scores. This
61 // behavior has changed. This test checks that if PositiveOnlyScoresFilter
62 // wraps one of these collectors, documents with <= 0 scores are indeed
65 int numPositiveScores = 0;
66 for (int i = 0; i < scores.length; i++) {
72 Directory directory = newDirectory();
73 RandomIndexWriter writer = new RandomIndexWriter(random, directory);
75 IndexReader ir = writer.getReader();
77 IndexSearcher searcher = newSearcher(ir);
78 Weight fake = new TermQuery(new Term("fake", "weight")).createWeight(searcher);
79 Scorer s = new SimpleScorer(fake);
80 TopDocsCollector<ScoreDoc> tdc = TopScoreDocCollector.create(scores.length, true);
81 Collector c = new PositiveScoresOnlyCollector(tdc);
83 while (s.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
86 TopDocs td = tdc.topDocs();
87 ScoreDoc[] sd = td.scoreDocs;
88 assertEquals(numPositiveScores, td.totalHits);
89 for (int i = 0; i < sd.length; i++) {
90 assertTrue("only positive scores should return: " + sd[i].score, sd[i].score > 0);