LUCENE-1379: fix corner case when Similarity.sloppyFreq() returns 0.0 the last matching doc is skipped

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@696013 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-09-16 19:25:07 +00:00
parent 088a42b846
commit 31811e9f45
2 changed files with 37 additions and 3 deletions

View File

@ -77,12 +77,12 @@ public class SpanScorer extends Scorer {
}
doc = spans.doc();
freq = 0.0f;
while (more && doc == spans.doc()) {
do {
int matchLength = spans.end() - spans.start();
freq += getSimilarity().sloppyFreq(matchLength);
more = spans.next();
}
return more || (freq != 0);
} while (more && (doc == spans.doc()));
return true;
}
public int doc() { return doc; }

View File

@ -20,6 +20,10 @@ package org.apache.lucene.search.spans;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.CheckHits;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
@ -355,4 +359,34 @@ public class TestSpans extends LuceneTestCase {
tstNextSpans(spans, 11, 5, 6);
assertFalse("final next", spans.next());
}
public void testSpanScorerZeroSloppyFreq() throws Exception {
boolean ordered = true;
int slop = 1;
final Similarity sim = new DefaultSimilarity() {
public float sloppyFreq(int distance) {
return 0.0f;
}
};
SpanNearQuery snq = new SpanNearQuery(
new SpanQuery[] {
makeSpanTermQuery("t1"),
makeSpanTermQuery("t2") },
slop,
ordered) {
public Similarity getSimilarity(Searcher s) {
return sim;
}
};
Scorer spanScorer = snq.weight(searcher).scorer(searcher.getIndexReader());
assertTrue("first doc", spanScorer.next());
assertEquals("first doc number", spanScorer.doc(), 11);
float score = spanScorer.score();
assertTrue("first doc score should be zero, " + score, score == 0.0f);
assertTrue("no second doc", ! spanScorer.next());
}
}