Skip to content

Commit

Permalink
Removded unnecessary call to find live docs during filtered search (#…
Browse files Browse the repository at this point in the history
…1177)

Signed-off-by: Navneet Verma <[email protected]>
(cherry picked from commit 78aba55)
  • Loading branch information
navneet1v authored and github-actions[bot] committed Oct 2, 2023
1 parent 47783ca commit 5ae70c6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
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

0 comments on commit 5ae70c6

Please sign in to comment.