Skip to content

Commit

Permalink
Optimize searchTopK method in ExactSearcher
Browse files Browse the repository at this point in the history
Signed-off-by: Junqiu Lei <junqiu@amazon.com>
  • Loading branch information
junqiu-lei committed Sep 24, 2024
1 parent 189562e commit 98c5a4b
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/main/java/org/opensearch/knn/index/query/ExactSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,25 @@ private Map<Integer, Float> scoreAllDocs(KNNIterator iterator) throws IOExceptio
}

private Map<Integer, Float> searchTopK(KNNIterator iterator, int k) throws IOException {
// Creating min heap and init with MAX DocID and Score as -INF.
final HitQueue queue = new HitQueue(k, true);
ScoreDoc topDoc = queue.top();
final Map<Integer, Float> docToScore = new HashMap<>();
int docId;

while ((docId = iterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
if (iterator.score() > topDoc.score) {
topDoc.score = iterator.score();
topDoc.doc = docId;
// As the HitQueue is min heap, updating top will bring the doc with -INF score or worst score we
// have seen till now on top.
topDoc = queue.updateTop();
float score = iterator.score();

if (score < 0) {
continue;
}
}

// If scores are negative we will remove them.
// This is done, because there can be negative values in the Heap as we init the heap with Score as -INF.
// If filterIds < k, the some values in heap can have a negative score.
while (queue.size() > 0 && queue.top().score < 0) {
queue.pop();
if (queue.size() < k) {
queue.add(new ScoreDoc(docId, score));
} else if (score > queue.top().score) {
ScoreDoc topDoc = queue.top();
topDoc.score = score;
topDoc.doc = docId;
queue.updateTop();
}
}

while (queue.size() > 0) {
Expand Down

0 comments on commit 98c5a4b

Please sign in to comment.