-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support $vectorSearch against nested embeddings and arrays of embeddings #2026
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
base: main
Are you sure you want to change the base?
Changes from all commits
d273191
a6b4bfe
910415b
d70ab35
60190f5
9756cb0
31784da
b87b717
2bbe312
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.mongodb.client.model.search; | ||
|
|
||
| import com.mongodb.annotations.Immutable; | ||
| import com.mongodb.internal.client.model.AbstractConstructibleBson; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.Document; | ||
| import org.bson.conversions.Bson; | ||
|
|
||
| import static com.mongodb.assertions.Assertions.notNull; | ||
|
|
||
| final class VectorSearchNestedConstructibleBson extends AbstractConstructibleBson<VectorSearchNestedConstructibleBson> | ||
| implements VectorSearchNestedOptions { | ||
| /** | ||
| * An {@linkplain Immutable immutable} {@link BsonDocument#isEmpty() empty} instance. | ||
| */ | ||
| static final VectorSearchNestedConstructibleBson EMPTY_IMMUTABLE = | ||
| new VectorSearchNestedConstructibleBson(AbstractConstructibleBson.EMPTY_IMMUTABLE); | ||
|
|
||
| VectorSearchNestedConstructibleBson(final Bson base) { | ||
| super(base); | ||
| } | ||
|
|
||
| private VectorSearchNestedConstructibleBson(final Bson base, final Document appended) { | ||
| super(base, appended); | ||
| } | ||
|
|
||
| @Override | ||
| protected VectorSearchNestedConstructibleBson newSelf(final Bson base, final Document appended) { | ||
| return new VectorSearchNestedConstructibleBson(base, appended); | ||
| } | ||
|
|
||
| @Override | ||
| public VectorSearchNestedOptions scoreMode(final VectorSearchScoreMode scoreMode) { | ||
| return newAppended("scoreMode", notNull("scoreMode", scoreMode).getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public VectorSearchNestedOptions option(final String name, final Object value) { | ||
| return newAppended(notNull("name", name), notNull("value", value)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.mongodb.client.model.search; | ||
|
|
||
| import com.mongodb.annotations.Sealed; | ||
| import org.bson.conversions.Bson; | ||
|
|
||
| /** | ||
| * Represents the optional {@code nestedOptions} sub-document of the {@code $vectorSearch} pipeline stage, | ||
| * used when searching against arrays of embeddings within nested (embedded) documents. | ||
| * | ||
| * @see VectorSearchOptions#nestedOptions(VectorSearchNestedOptions) | ||
| * @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch | ||
| * @mongodb.server.release 8.3 | ||
| * @since 5.10 | ||
| */ | ||
| @Sealed | ||
| public interface VectorSearchNestedOptions extends Bson { | ||
| /** | ||
| * Creates a new {@link VectorSearchNestedOptions} with the score aggregation mode specified. | ||
| * | ||
| * @param scoreMode The score aggregation mode for the matching embeddings within a document. | ||
| * @return A new {@link VectorSearchNestedOptions}. | ||
| */ | ||
| VectorSearchNestedOptions scoreMode(VectorSearchScoreMode scoreMode); | ||
|
|
||
| /** | ||
| * Creates a new {@link VectorSearchNestedOptions} with the specified option in situations when there is no | ||
| * builder method that better satisfies your needs. | ||
| * This method cannot be used to validate the syntax. | ||
| * <p> | ||
| * <i>Example</i><br> | ||
| * The following code creates two functionally equivalent {@link VectorSearchNestedOptions} objects, | ||
| * though they may not be {@linkplain Object#equals(Object) equal}. | ||
| * <pre>{@code | ||
| * VectorSearchNestedOptions options1 = VectorSearchNestedOptions.vectorSearchNestedOptions() | ||
| * .scoreMode(VectorSearchScoreMode.AVG); | ||
| * VectorSearchNestedOptions options2 = VectorSearchNestedOptions.vectorSearchNestedOptions() | ||
| * .option("scoreMode", "avg"); | ||
| * }</pre> | ||
| * | ||
| * @param name The option name. | ||
| * @param value The option value. | ||
| * @return A new {@link VectorSearchNestedOptions}. | ||
| */ | ||
| VectorSearchNestedOptions option(String name, Object value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like from the docs https://www.mongodb.com/docs/vector-search/query/aggregation-stages/vector-search-stage/?deployment-type=atlas&embedding=byo&interface=driver&language=nodejs
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this may change over time I opted to add to keep in line with the other Search option classes. |
||
|
|
||
| /** | ||
| * Returns {@link VectorSearchNestedOptions} that represents server defaults. | ||
| * | ||
| * @return {@link VectorSearchNestedOptions} that represents server defaults. | ||
| */ | ||
| static VectorSearchNestedOptions vectorSearchNestedOptions() { | ||
| return VectorSearchNestedConstructibleBson.EMPTY_IMMUTABLE; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ | |
| /** | ||
| * Represents optional fields of the {@code $vectorSearch} pipeline stage of an aggregation pipeline. | ||
| * | ||
| * <p>This includes options for searching against nested (embedded) embeddings and arrays of embeddings, | ||
| * via {@link #parentFilter(Bson)} and {@link #nestedOptions(VectorSearchNestedOptions)}.</p> | ||
| * | ||
| * @see Aggregates#vectorSearch(FieldSearchPath, Iterable, String, long, VectorSearchOptions) | ||
| * @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch | ||
| * @mongodb.server.release 6.0.11 | ||
|
|
@@ -41,6 +44,41 @@ public interface VectorSearchOptions extends Bson { | |
| */ | ||
| VectorSearchOptions filter(Bson filter); | ||
|
|
||
| /** | ||
| * Creates a new {@link VectorSearchOptions} with the parent filter specified. | ||
| * | ||
| * <p>Applies only when searching against a nested (embedded) field: this filter is applied to the | ||
| * parent (root) documents, while {@link #filter(Bson)} is applied to the leaf (embedded) documents. | ||
| * For a non-nested search, use {@link #filter(Bson)} instead.</p> | ||
| * <p>Unlike some other MongoDB drivers, the Java driver does not automatically re-route a top-level | ||
| * {@link #filter(Bson)} to {@code parentFilter} based on the path: you must call | ||
| * {@link #parentFilter(Bson)} explicitly to specify a parent (root-level) filter for a nested search.</p> | ||
| * | ||
| * @param parentFilter A filter applied to the parent documents of a nested {@code $vectorSearch}. | ||
| * One may use {@link Filters} to create this filter, though not all filters may be supported. | ||
| * See the MongoDB documentation for the list of supported filters. | ||
| * @return A new {@link VectorSearchOptions}. | ||
| * @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch | ||
| * @mongodb.server.release 8.3 | ||
| * @since 5.10 | ||
| */ | ||
| VectorSearchOptions parentFilter(Bson parentFilter); | ||
|
|
||
| /** | ||
| * Creates a new {@link VectorSearchOptions} with the {@code nestedOptions} specified. | ||
| * | ||
| * <p>Applies only when searching against arrays of embeddings within nested (embedded) documents; | ||
| * for example it controls how the scores of the individual matching embeddings within a document are | ||
| * aggregated (see {@link VectorSearchNestedOptions#scoreMode(VectorSearchScoreMode)}).</p> | ||
| * | ||
| * @param nestedOptions The options for a nested (embedded) {@code $vectorSearch}. | ||
| * @return A new {@link VectorSearchOptions}. | ||
| * @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch | ||
| * @mongodb.server.release 8.3 | ||
| * @since 5.10 | ||
| */ | ||
| VectorSearchOptions nestedOptions(VectorSearchNestedOptions nestedOptions); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not relevant, I double checked and found |
||
|
|
||
| /** | ||
| * Creates a new {@link VectorSearchOptions} that instructs to return only stored source fields. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.mongodb.client.model.search; | ||
|
|
||
| /** | ||
| * The score aggregation mode for a {@code $vectorSearch} against arrays of embeddings in nested (embedded) documents. | ||
| * | ||
| * <p>If {@code scoreMode} is not specified, the server default is {@link #MAX} ({@code "max"}).</p> | ||
| * | ||
| * @see VectorSearchNestedOptions#scoreMode(VectorSearchScoreMode) | ||
| * @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch | ||
| * @mongodb.server.release 8.3 | ||
| * @since 5.10 | ||
| */ | ||
| public enum VectorSearchScoreMode { | ||
| /** | ||
| * Use the average of the scores of the matching embeddings within a document. | ||
| */ | ||
| AVG("avg"), | ||
|
|
||
| /** | ||
| * Use the maximum of the scores of the matching embeddings within a document. | ||
| */ | ||
| MAX("max"); | ||
|
|
||
| private final String value; | ||
|
|
||
| VectorSearchScoreMode(final String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value used by the server for this score mode. | ||
| * | ||
| * @return the server value ({@code "avg"} or {@code "max"}). | ||
| * @since 5.10 | ||
| */ | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the whole SearchOptions file is annotated
@Beta(Reason.CLIENT)