Skip to content

Commit

Permalink
Enabled Filtering on Nested Vector fields with top level filters
Browse files Browse the repository at this point in the history
Signed-off-by: Navneet Verma <navneev@amazon.com>
  • Loading branch information
navneet1v committed Jan 5, 2024
1 parent 5c24d99 commit 9bf2e60
Show file tree
Hide file tree
Showing 7 changed files with 754 additions and 57 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Maintenance
### Refactoring

## [Unreleased 2.x](https://github.com/opensearch-project/k-NN/compare/2.11...2.x)
## [Unreleased 2.x](https://github.com/opensearch-project/k-NN/compare/2.12...2.x)
### Features
* Add parent join support for lucene knn [#1182](https://github.com/opensearch-project/k-NN/pull/1182)
### Enhancements
* Increase Lucene max dimension limit to 16,000 [#1346](https://github.com/opensearch-project/k-NN/pull/1346)
* Tuned default values for ef_search and ef_construction for better indexing and search performance for vector search [#1353](https://github.com/opensearch-project/k-NN/pull/1353)
* Enabled Filtering on Nested Vector fields with top level filters [#1372](https://github.com/opensearch-project/k-NN/pull/1372)
### Bug Fixes
* Fix use-after-free case on nmslib search path [#1305](https://github.com/opensearch-project/k-NN/pull/1305)
* Allow nested knn field mapping when train model [#1318](https://github.com/opensearch-project/k-NN/pull/1318)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.search.join.DiversifyingChildrenByteKnnVectorQuery;
import org.apache.lucene.search.join.DiversifyingChildrenFloatKnnVectorQuery;
import org.apache.lucene.search.join.ToChildBlockJoinQuery;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.search.NestedHelper;
import org.opensearch.knn.index.VectorDataType;
import org.opensearch.knn.index.util.KNNEngine;

Expand Down Expand Up @@ -155,11 +157,27 @@ private static Query getFilterQuery(CreateQueryRequest createQueryRequest) {
createQueryRequest.k
)
);
final Query filterQuery;
try {
return createQueryRequest.getFilter().get().toQuery(queryShardContext);
filterQuery = createQueryRequest.getFilter().get().toQuery(queryShardContext);
} catch (IOException e) {
throw new RuntimeException("Cannot create knn query with filter", e);
}
// If k-NN Field is nested field then parentFilter will not be null. This parentFilter is set by the
// Opensearch core. Ref PR: https://github.com/opensearch-project/OpenSearch/pull/10246
if (queryShardContext.getParentFilter() != null) {
// if the filter is also a nested query clause then we should just return the same query without
// considering it to join with the parent documents.
if (new NestedHelper(queryShardContext.getMapperService()).mightMatchNestedDocs(filterQuery)) {
return filterQuery;
}
// This condition will be hit when filters are getting applied on the top level fields and k-nn
// query field is a nested field. In this case we need to wrap the filter query with
// ToChildBlockJoinQuery to ensure parent documents which will be retrieved from filters can be
// joined with the child documents containing vector field.
return new ToChildBlockJoinQuery(filterQuery, queryShardContext.getParentFilter());
}
return filterQuery;
}
return null;
}
Expand Down
Loading

0 comments on commit 9bf2e60

Please sign in to comment.