Support $vectorSearch against nested embeddings and arrays of embeddings - #2026
Support $vectorSearch against nested embeddings and arrays of embeddings#2026rozza wants to merge 9 commits into
Conversation
… of embeddings Add query-time $vectorSearch options for nested (embedded) and arrays-of embeddings: - VectorSearchOptions.parentFilter(Bson): parent/root-level filter for a nested search (the existing filter(Bson) targets the leaf/embedded docs). - VectorSearchOptions.nestedOptions(VectorSearchNestedOptions): the nestedOptions sub-document. - VectorSearchNestedOptions: an extensible @Sealed options type with a typed scoreMode(VectorSearchScoreMode) helper and an option(String, Object) escape hatch, so future server fields are additive. - VectorSearchScoreMode enum (avg/max) for embedded score aggregation. Follows the existing self-rendering immutable options-builder pattern; no path introspection. Index-time nesting needs no new API (SearchIndexModel accepts a raw Bson definition).
The Java VectorSearchOptions, ApproximateVectorSearchOptions and ExactVectorSearchOptions interfaces are stable (@Sealed, not @beta), but their Scala type aliases still carried @beta(Reason.SERVER) left over from when the feature was in beta. Drop the stale annotations so the Scala surface matches the Java classes.
Adding a public Java type/enum under a wrapped package requires a matching Scala wrapper (enforced by ApiAliasAndCompanionSpec): a type alias with stability annotations kept in sync with the Java class, a companion object re-exposing enum constants, and factory objects following each package's convention.
There was a problem hiding this comment.
Pull request overview
Adds support for $vectorSearch against nested embeddings and arrays of embeddings by introducing new Java model types (VectorSearchNestedOptions, VectorSearchScoreMode) and exposing them through the Scala driver’s mirrored API surface. Also aligns a few stability annotations (@Beta) with their Java counterparts and updates the Scala mirroring enforcement.
Changes:
- Add
parentFilterandnestedOptionssupport toVectorSearchOptions, including BSON serialization and unit tests. - Introduce
VectorSearchNestedOptionsandVectorSearchScoreModeindriver-core, and mirror them indriver-scala(type aliases + companion objects). - Clean up/realign
@Betausage and strengthen Scala mirror enforcement to include enums.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| driver-scala/src/test/scala-2/org/mongodb/scala/ApiAliasAndCompanionSpec.scala | Updates mirror test to include enums when comparing Java vs Scala API surfaces. |
| driver-scala/src/main/scala/org/mongodb/scala/model/search/VectorSearchScoreMode.scala | Scala companion object re-exposing Java enum constants for term-scope access. |
| driver-scala/src/main/scala/org/mongodb/scala/model/search/VectorSearchNestedOptions.scala | Scala companion object exposing Java factory method for nested options. |
| driver-scala/src/main/scala/org/mongodb/scala/model/search/package.scala | Removes stale @Beta on vector search option aliases; adds new aliases for nested options + score mode. |
| driver-scala/AGENTS.md | Documents/enforces Scala mirroring rules and @Beta sync expectations. |
| driver-core/src/test/unit/com/mongodb/client/model/search/VectorSearchOptionsTest.java | Adds unit tests covering parentFilter and nestedOptions BSON output and null checks. |
| driver-core/src/test/unit/com/mongodb/client/model/search/VectorSearchNestedOptionsTest.java | Adds unit tests for nested options BSON output, last-write-wins, and immutability behavior. |
| driver-core/src/main/com/mongodb/client/model/search/VectorSearchScoreMode.java | New public enum defining server string values for score aggregation. |
| driver-core/src/main/com/mongodb/client/model/search/VectorSearchOptions.java | Adds parentFilter and nestedOptions APIs with documentation. |
| driver-core/src/main/com/mongodb/client/model/search/VectorSearchNestedOptions.java | New public options interface with builder methods and static factory. |
| driver-core/src/main/com/mongodb/client/model/search/VectorSearchNestedConstructibleBson.java | Implements BSON-building for nested options (scoreMode/option). |
| driver-core/src/main/com/mongodb/client/model/search/VectorSearchConstructibleBson.java | Implements new parentFilter / nestedOptions methods in the existing constructible options. |
| driver-core/src/main/com/mongodb/client/model/search/SearchOptions.java | Removes method-level @Beta({CLIENT, SERVER}) to reflect updated stability semantics. |
| driver-core/src/main/com/mongodb/client/model/Aggregates.java | Expands $vectorSearch Javadoc to describe nested search behavior and the new options. |
Comments suppressed due to low confidence (1)
driver-core/src/main/com/mongodb/client/model/Aggregates.java:961
- These Javadoc links rely on imports that are otherwise unused in this file. If you remove the unused imports, update the links to use fully-qualified type names so Javadoc generation can still resolve them.
* {@link VectorSearchOptions#nestedOptions(VectorSearchNestedOptions)} (e.g.
* {@link VectorSearchNestedOptions#scoreMode(VectorSearchScoreMode)}) to control score aggregation.</p>
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Avoids scanning com.mongodb.client.model.search twice when collecting both Object and Enum subtypes.
|
Added JAVA-6263 - for tracking integration tests. |
| * @param option The counting option. | ||
| * @return A new {@link SearchOptions}. | ||
| */ | ||
| @Beta({Reason.CLIENT, Reason.SERVER}) |
There was a problem hiding this comment.
Note the whole SearchOptions file is annotated @Beta(Reason.CLIENT)
| } | ||
|
|
||
| @Test | ||
| void vectorSearchNestedOptionsIsUnmodifiable() { |
There was a problem hiding this comment.
I'm not 100% sold on the value of this test and the one below - but both SearchOptionsTest and FuzzySearchOptionsTest have the same tests.
| * @since 4.11 | ||
| */ | ||
| @Sealed | ||
| @Beta(Array(Reason.SERVER)) |
There was a problem hiding this comment.
Keeping Scala types inline with the Java package. I also added a rule to AGENTS.md
So the test class maps directly to the VectorSearchOptions interface it covers, matching the rest of the package.
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| final class BinaryVectorSearchOptionsTest { | ||
| final class VectorSearchOptionsTest { |
There was a problem hiding this comment.
Opted to rename so to follow the existing convention of <Name>Test
JAVA-5987
Also some
@Betaannotations clean ups