diff --git a/lucene/core/src/java/org/apache/lucene/search/TwoPhaseDocIdSetIterator.java b/lucene/core/src/java/org/apache/lucene/search/TwoPhaseDocIdSetIterator.java index baad57fb437..8c370d36035 100644 --- a/lucene/core/src/java/org/apache/lucene/search/TwoPhaseDocIdSetIterator.java +++ b/lucene/core/src/java/org/apache/lucene/search/TwoPhaseDocIdSetIterator.java @@ -73,9 +73,9 @@ public abstract class TwoPhaseDocIdSetIterator { public abstract DocIdSetIterator approximation(); /** Return whether the current doc ID that the iterator is on matches. This - * method should only be called when the iterator is positionned, ie. not + * method should only be called when the iterator is positionned -- ie. not * when {@link DocIdSetIterator#docID()} is {@code -1} or - * {@link DocIdSetIterator#NO_MORE_DOCS}. */ + * {@link DocIdSetIterator#NO_MORE_DOCS} -- and at most once. */ public abstract boolean matches() throws IOException; } diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java b/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java index 62bd77ef73d..0bd63e24dcf 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java +++ b/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java @@ -188,6 +188,7 @@ public class RandomApproximationQuery extends Query { private final DocIdSetIterator disi; private final RandomApproximation approximation; + private int lastDoc = -1; RandomTwoPhaseView(Random random, DocIdSetIterator disi) { this.disi = disi; @@ -201,7 +202,14 @@ public class RandomApproximationQuery extends Query { @Override public boolean matches() throws IOException { - return approximation.doc == disi.docID(); + if (approximation.docID() == -1 || approximation.docID() == DocIdSetIterator.NO_MORE_DOCS) { + throw new AssertionError("matches() should not be called on doc ID " + approximation.doc); + } + if (lastDoc == approximation.docID()) { + throw new AssertionError("matches() has been called twice on doc ID " + approximation.doc); + } + lastDoc = approximation.docID(); + return approximation.docID() == disi.docID(); } }