Skip to content

Apache Lucene open-source search software

LUCENE-8196: Check that the TermIntervalsSource is positioned on the correct term

Alan Woodward1d6502cecb94
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/intervals/TermIntervalsSource.java b/lucene/sandbox/src/java/org/apache/lucene/search/intervals/TermIntervalsSource.java
index b375a0102ad..81578ef02cb 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/intervals/TermIntervalsSource.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/intervals/TermIntervalsSource.java
@@ -50,7 +50,9 @@ class TermIntervalsSource extends IntervalsSource {
throw new IllegalArgumentException("Cannot create an IntervalIterator over field " + field + " because it has no indexed positions");
}
TermsEnum te = terms.iterator();
- te.seekExact(term);
+ if (te.seekExact(term) == false) {
+ return null;
+ }
PostingsEnum pe = te.postings(null, PostingsEnum.POSITIONS);
float cost = termPositionsCost(te);
return new IntervalIterator() {
diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/intervals/TestIntervalQuery.java b/lucene/sandbox/src/test/org/apache/lucene/search/intervals/TestIntervalQuery.java
index a81ab76682b..de7341e9bc4 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/search/intervals/TestIntervalQuery.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/search/intervals/TestIntervalQuery.java
@@ -149,6 +149,11 @@ public class TestIntervalQuery extends LuceneTestCase {
checkHits(q, new int[]{ 1, 3, 4, 5 });
}
+ public void testNonExistentTerms() throws IOException {
+ Query q = new IntervalQuery(field, Intervals.ordered(Intervals.term("w0"), Intervals.term("w2")));
+ checkHits(q, new int[]{});
+ }
+
// The Vigna paper doesn't deal with prefix disjunctions. For now, we keep the same
// logic as detailed in the paper, but we may want to address it in future so that tests
// like the one below will pass