Skip to content

Commit

Permalink
Make the HitQueue size more appropriate for KNN exact search (#13184)
Browse files Browse the repository at this point in the history
Currently, when performing KNN exact search, we consistently set the HitQueue size to k. However, there may be instances where the number of candidates is actually lower than k.
  • Loading branch information
bugmakerrrrrr committed Mar 19, 2024
1 parent d393b9d commit 7a08eea
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Optimizations

* GITHUB#13121: Speedup multi-segment HNSW graph search for diversifying child kNN queries. Builds on GITHUB#12962.
(Ben Trent)
* GITHUB#13184: Make the HitQueue size more appropriate for KNN exact search (Pan Guixin)

Bug Fixes
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ protected TopDocs exactSearch(LeafReaderContext context, DocIdSetIterator accept
if (vectorScorer == null) {
return NO_RESULTS;
}
HitQueue queue = new HitQueue(k, true);
final int queueSize = Math.min(k, Math.toIntExact(acceptIterator.cost()));
HitQueue queue = new HitQueue(queueSize, true);
ScoreDoc topDoc = queue.top();
int doc;
while ((doc = acceptIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ protected TopDocs exactSearch(LeafReaderContext context, DocIdSetIterator accept
parentBitSet,
query,
fi.getVectorSimilarityFunction());
HitQueue queue = new HitQueue(k, true);
final int queueSize = Math.min(k, Math.toIntExact(acceptIterator.cost()));
HitQueue queue = new HitQueue(queueSize, true);
ScoreDoc topDoc = queue.top();
while (vectorScorer.nextParent() != DocIdSetIterator.NO_MORE_DOCS) {
float score = vectorScorer.score();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ protected TopDocs exactSearch(LeafReaderContext context, DocIdSetIterator accept
parentBitSet,
query,
fi.getVectorSimilarityFunction());
HitQueue queue = new HitQueue(k, true);
final int queueSize = Math.min(k, Math.toIntExact(acceptIterator.cost()));
HitQueue queue = new HitQueue(queueSize, true);
ScoreDoc topDoc = queue.top();
while (vectorScorer.nextParent() != DocIdSetIterator.NO_MORE_DOCS) {
float score = vectorScorer.score();
Expand Down

0 comments on commit 7a08eea

Please sign in to comment.