diff --git a/lucene/core/src/java/org/apache/lucene/search/AbstractVectorSimilarityQuery.java b/lucene/core/src/java/org/apache/lucene/search/AbstractVectorSimilarityQuery.java index 393d4b5731f..03dea82578c 100644 --- a/lucene/core/src/java/org/apache/lucene/search/AbstractVectorSimilarityQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/AbstractVectorSimilarityQuery.java @@ -255,6 +255,11 @@ abstract class AbstractVectorSimilarityQuery extends Query { new FilteredDocIdSetIterator(acceptDocs) { @Override protected boolean match(int doc) throws IOException { + // Advance the scorer + if (!scorer.advanceExact(doc)) { + return false; + } + // Compute the dot product float score = scorer.score(); cachedScore[0] = score * boost; diff --git a/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java b/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java index 4c61ad17af9..29b76e53af2 100644 --- a/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java @@ -87,6 +87,7 @@ abstract class VectorScorer { @Override public float score() throws IOException { + assert values.docID() != -1 : getClass().getSimpleName() + " is not positioned"; return similarity.compare(query, values.vectorValue()); } } @@ -117,6 +118,7 @@ abstract class VectorScorer { @Override public float score() throws IOException { + assert values.docID() != -1 : getClass().getSimpleName() + " is not positioned"; return similarity.compare(query, values.vectorValue()); } } diff --git a/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java b/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java index 98a1e177948..c4f624bae74 100644 --- a/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java +++ b/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java @@ -433,8 +433,8 @@ abstract class BaseVectorSimilarityQueryTestCase< public void testApproximate() throws IOException { // Non-restrictive filter, along with similarity to visit a small number of nodes - int numFiltered = random().nextInt((numDocs * 4) / 5, numDocs); - int targetVisited = random().nextInt(numFiltered / 10, numFiltered / 8); + int numFiltered = numDocs - 1; + int targetVisited = random().nextInt(1, numFiltered / 10); V[] vectors = getRandomVectors(numDocs, dim); V queryVector = getRandomVector(dim);