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

Removed unnecessary call to find live docs during filtered search #1177

Merged
merged 1 commit into from
Oct 2, 2023
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
15 changes: 3 additions & 12 deletions src/main/java/org/opensearch/knn/index/query/KNNWeight.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public Scorer scorer(LeafReaderContext context) throws IOException {
* . Hence, if filtered results are less than K and filter query is present we should shift to exact search.
* This improves the recall.
*/
if (filterWeight != null && canDoExactSearch(filterIdsArray.length, getTotalDocsInSegment(context))) {
if (filterWeight != null && canDoExactSearch(filterIdsArray.length)) {
docIdsToScoreMap.putAll(doExactSearch(context, filterIdsArray));
} else {
Map<Integer, Float> annResults = doANNSearch(context, filterIdsArray);
Expand Down Expand Up @@ -380,10 +380,9 @@ private SpaceType getSpaceType(final FieldInfo fieldInfo) {
);
}

private boolean canDoExactSearch(final int filterIdsCount, final int searchableDocs) {
private boolean canDoExactSearch(final int filterIdsCount) {
log.debug(
"Info for doing exact search Live Docs: {}, filterIdsLength : {}, Threshold value: {}",
searchableDocs,
"Info for doing exact search filterIdsLength : {}, Threshold value: {}",
filterIdsCount,
KNNSettings.getFilteredExactSearchThreshold(knnQuery.getIndexName())
);
Expand Down Expand Up @@ -420,12 +419,4 @@ private boolean isExactSearchThresholdSettingSet(int filterThresholdValue) {
private boolean canDoExactSearchAfterANNSearch(final int filterIdsCount, final int annResultCount) {
return filterWeight != null && filterIdsCount >= knnQuery.getK() && knnQuery.getK() > annResultCount;
}

private int getTotalDocsInSegment(final LeafReaderContext context) {
// This means that there is no deleted documents, hence the live docs bitset is null
if (context.reader().getLiveDocs() == null) {
return context.reader().maxDoc();
}
return context.reader().getLiveDocs().length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ public void testANNWithFilterQuery_whenExactSearch_thenSuccess() {
final Bits liveDocsBits = mock(Bits.class);
when(reader.getLiveDocs()).thenReturn(liveDocsBits);
when(liveDocsBits.get(filterDocId)).thenReturn(true);
when(liveDocsBits.length()).thenReturn(1000);

final KNNWeight knnWeight = new KNNWeight(query, 0.0f, filterQueryWeight);
final Map<String, String> attributesMap = ImmutableMap.of(KNN_ENGINE, KNNEngine.FAISS.getName(), SPACE_TYPE, SpaceType.L2.name());
Expand Down Expand Up @@ -476,7 +475,6 @@ public void testANNWithFilterQuery_whenExactSearchAndThresholdComputations_thenS
final Bits liveDocsBits = mock(Bits.class);
when(reader.getLiveDocs()).thenReturn(liveDocsBits);
when(liveDocsBits.get(filterDocId)).thenReturn(true);
when(liveDocsBits.length()).thenReturn(1000);

final KNNWeight knnWeight = new KNNWeight(query, 0.0f, filterQueryWeight);
final Map<String, String> attributesMap = ImmutableMap.of(KNN_ENGINE, KNNEngine.FAISS.getName(), SPACE_TYPE, SpaceType.L2.name());
Expand Down
Loading