Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the HitQueue size more appropriate for exact search #1549

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased 2.x](https://github.com/opensearch-project/k-NN/compare/2.13...2.x)
### Features
### Enhancements
* Make the HitQueue size more appropriate for exact search [#1549](https://github.com/opensearch-project/k-NN/pull/1549)
### Bug Fixes
### Infrastructure
### Documentation
Expand Down
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 @@
* 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 @@
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 @@ -309,10 +309,10 @@
.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
Loading