Skip to content

Commit 26402b2

Browse files
authored
Specialise the wrapping of vector scorer iterator for KnnVectorValues.DocIndexIterator in ExitableDirectoryReader (#131623)
This commit specialises the wrapping of vector scorer iterator for KnnVectorValues.DocIndexIterator in ExitableDirectoryReader. This change is necessary now since a new assert in Lucene catches this scenario, since DocIndexIterator is expected in several places which we were previously not exposing. DocIndexIterator can be optimized more than DocIdSetIterator. closes #131610
1 parent 694cfba commit 26402b2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

server/src/main/java/org/elasticsearch/search/internal/ExitableDirectoryReader.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,9 @@ public VectorScorer scorer(byte[] bytes) throws IOException {
586586
if (scorer == null) {
587587
return null;
588588
}
589+
DocIdSetIterator scorerIterator = scorer.iterator();
589590
return new VectorScorer() {
590-
private final DocIdSetIterator iterator = new ExitableDocSetIterator(scorer.iterator(), queryCancellation);
591+
private final DocIdSetIterator iterator = exitableIterator(scorerIterator, queryCancellation);
591592

592593
@Override
593594
public float score() throws IOException {
@@ -637,8 +638,9 @@ public VectorScorer scorer(float[] target) throws IOException {
637638
if (scorer == null) {
638639
return null;
639640
}
641+
DocIdSetIterator scorerIterator = scorer.iterator();
640642
return new VectorScorer() {
641-
private final DocIdSetIterator iterator = new ExitableDocSetIterator(scorer.iterator(), queryCancellation);
643+
private final DocIdSetIterator iterator = exitableIterator(scorerIterator, queryCancellation);
642644

643645
@Override
644646
public float score() throws IOException {
@@ -663,6 +665,15 @@ public FloatVectorValues copy() throws IOException {
663665
}
664666
}
665667

668+
/** Wraps the iterator in an exitable iterator, specializing for KnnVectorValues.DocIndexIterator. */
669+
static DocIdSetIterator exitableIterator(DocIdSetIterator iterator, QueryCancellation queryCancellation) {
670+
if (iterator instanceof KnnVectorValues.DocIndexIterator docIndexIterator) {
671+
return createExitableIterator(docIndexIterator, queryCancellation);
672+
} else {
673+
return new ExitableDocSetIterator(iterator, queryCancellation);
674+
}
675+
}
676+
666677
private static KnnVectorValues.DocIndexIterator createExitableIterator(
667678
KnnVectorValues.DocIndexIterator delegate,
668679
QueryCancellation queryCancellation

0 commit comments

Comments
 (0)