Skip to content

Commit

Permalink
Make the HitQueue size more appropriate for exact search
Browse files Browse the repository at this point in the history
Signed-off-by: panguixin <[email protected]>
  • Loading branch information
bugmakerrrrrr committed Mar 16, 2024
1 parent 4734d88 commit 8eec69c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/opensearch/knn/index/query/KNNWeight.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Scorer scorer(LeafReaderContext context) throws IOException {
* This improves the recall.
*/
if (filterWeight != null && canDoExactSearch(cardinality)) {
docIdsToScoreMap.putAll(doExactSearch(context, filterBitSet));
docIdsToScoreMap.putAll(doExactSearch(context, filterBitSet, cardinality));
} else {
Map<Integer, Float> annResults = doANNSearch(context, filterBitSet, cardinality);
if (annResults == null) {
Expand All @@ -131,7 +131,7 @@ public Scorer scorer(LeafReaderContext context) throws IOException {
annResults.size(),
cardinality
);
annResults = doExactSearch(context, filterBitSet);
annResults = doExactSearch(context, filterBitSet, cardinality);

Check warning on line 134 in src/main/java/org/opensearch/knn/index/query/KNNWeight.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/knn/index/query/KNNWeight.java#L134

Added line #L134 was not covered by tests
}
docIdsToScoreMap.putAll(annResults);
}
Expand Down Expand Up @@ -308,10 +308,10 @@ private Map<Integer, Float> doANNSearch(final LeafReaderContext context, final B
.collect(Collectors.toMap(KNNQueryResult::getId, result -> knnEngine.score(result.getScore(), spaceType)));
}

private Map<Integer, Float> doExactSearch(final LeafReaderContext leafReaderContext, final BitSet filterIdsBitSet) {
private Map<Integer, Float> doExactSearch(final LeafReaderContext leafReaderContext, final BitSet filterIdsBitSet, int cardinality) {
try {
// Creating min heap and init with MAX DocID and Score as -INF.
final HitQueue queue = new HitQueue(this.knnQuery.getK(), true);
final HitQueue queue = new HitQueue(Math.min(this.knnQuery.getK(), cardinality), true);
ScoreDoc topDoc = queue.top();
final Map<Integer, Float> docToScore = new HashMap<>();
FilteredIdsKNNIterator iterator = getFilteredKNNIterator(leafReaderContext, filterIdsBitSet);
Expand Down

0 comments on commit 8eec69c

Please sign in to comment.