-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Introduce builder for $vectorSearch
aggregation stage
#1200
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
Changes from all commits
b183f1b
926b519
d9b071b
66e3364
02d76f7
e67c262
44909a1
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package com.mongodb.client.model; | ||
|
||
import com.mongodb.annotations.Beta; | ||
import com.mongodb.client.model.geojson.Geometry; | ||
import com.mongodb.client.model.geojson.Point; | ||
import com.mongodb.client.model.search.SearchCollector; | ||
|
@@ -84,11 +85,30 @@ public static <TItem> Bson eq(@Nullable final TItem value) { | |
* @param <TItem> the value type | ||
* @return the filter | ||
* @mongodb.driver.manual reference/operator/query/eq $eq | ||
* @see #eqFull(String, Object) | ||
*/ | ||
public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) { | ||
return new SimpleEncodingFilter<>(fieldName, value); | ||
} | ||
|
||
/** | ||
* Creates a filter that matches all documents where the value of the field name equals the specified value. | ||
* Unlike {@link #eq(String, Object)}, this method creates a full form of {@code $eq}. | ||
* This method exists temporarily until Atlas starts supporting the short form of {@code $eq}. | ||
* It will likely be removed in the next driver release. | ||
* | ||
* @param fieldName the field name | ||
* @param value the value, which may be null | ||
* @param <TItem> the value type | ||
* @return the filter | ||
* @mongodb.driver.manual reference/operator/query/eq $eq | ||
* @since 4.11 | ||
*/ | ||
@Beta(Beta.Reason.SERVER) | ||
public static <TItem> Bson eqFull(final String fieldName, @Nullable final TItem value) { | ||
return new OperatorFilter<>("$eq", fieldName, value); | ||
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.
P.S. More thoughts on the current approach, which turned out to be unviable anyway. On one hand, nobody should care whether we use a full form or not. On the other hand, for some reason we documented that we are not using the full form. This, combined with the regex behavior, allows users to abuse 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 supporting the short 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. Can we annotate this as Beta, and remove it once Atlas supports the short form? 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. Alexander Lukyanchikov said that allAtlas clusters will get the updated behavior, unless they are "pinned on an older version for some reason" (I am not even sure if this can be done by a user). So this should work. 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. A ticket to remove |
||
} | ||
|
||
/** | ||
* Creates a filter that matches all documents where the value of the field name does not equal the specified value. | ||
* | ||
|
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; | ||
|
||
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 VectorSearchConstructibleBson extends AbstractConstructibleBson<VectorSearchConstructibleBson> implements VectorSearchOptions { | ||
/** | ||
* An {@linkplain Immutable immutable} {@link BsonDocument#isEmpty() empty} instance. | ||
*/ | ||
static final VectorSearchConstructibleBson EMPTY_IMMUTABLE = new VectorSearchConstructibleBson(AbstractConstructibleBson.EMPTY_IMMUTABLE); | ||
|
||
VectorSearchConstructibleBson(final Bson base) { | ||
super(base); | ||
} | ||
|
||
private VectorSearchConstructibleBson(final Bson base, final Document appended) { | ||
super(base, appended); | ||
} | ||
|
||
@Override | ||
protected VectorSearchConstructibleBson newSelf(final Bson base, final Document appended) { | ||
return new VectorSearchConstructibleBson(base, appended); | ||
} | ||
|
||
@Override | ||
public VectorSearchOptions filter(final Bson filter) { | ||
return newAppended("filter", notNull("name", filter)); | ||
} | ||
|
||
@Override | ||
public VectorSearchOptions 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,77 @@ | ||
/* | ||
* 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.Beta; | ||
import com.mongodb.annotations.Sealed; | ||
import com.mongodb.client.model.Aggregates; | ||
import com.mongodb.client.model.Filters; | ||
import org.bson.conversions.Bson; | ||
|
||
/** | ||
* Represents optional fields of the {@code $vectorSearch} pipeline stage of an aggregation pipeline. | ||
* | ||
* @see Aggregates#vectorSearch(FieldSearchPath, Iterable, String, long, long, VectorSearchOptions) | ||
* @mongodb.atlas.manual atlas-vector-search/vector-search-stage/ $vectorSearch | ||
* @mongodb.server.release 6.0.10 | ||
* @since 4.11 | ||
*/ | ||
@Sealed | ||
@Beta(Beta.Reason.SERVER) | ||
public interface VectorSearchOptions extends Bson { | ||
/** | ||
* Creates a new {@link VectorSearchOptions} with the filter specified. | ||
* | ||
* @param filter A filter that is applied before applying the | ||
* {@link Aggregates#vectorSearch(FieldSearchPath, Iterable, String, long, long, VectorSearchOptions) queryVector}. | ||
* 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. | ||
* <p> | ||
* Note that for now one has to use {@link Filters#eqFull(String, Object)} instead of {@link Filters#eq(String, Object)}.</p> | ||
* @return A new {@link VectorSearchOptions}. | ||
*/ | ||
VectorSearchOptions filter(Bson filter); | ||
|
||
/** | ||
* Creates a new {@link VectorSearchOptions} 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 VectorSearchOptions} objects, | ||
* though they may not be {@linkplain Object#equals(Object) equal}. | ||
* <pre>{@code | ||
* VectorSearchOptions options1 = VectorSearchOptions.vectorSearchOptions() | ||
* .filter(Filters.lt("fieldName", 1)); | ||
* VectorSearchOptions options2 = VectorSearchOptions.vectorSearchOptions() | ||
* .option("filter", Filters.lt("fieldName", 1)); | ||
* }</pre> | ||
* | ||
* @param name The option name. | ||
* @param value The option value. | ||
* @return A new {@link VectorSearchOptions}. | ||
*/ | ||
VectorSearchOptions option(String name, Object value); | ||
|
||
/** | ||
* Returns {@link VectorSearchOptions} that represents server defaults. | ||
* | ||
* @return {@link VectorSearchOptions} that represents server defaults. | ||
*/ | ||
static VectorSearchOptions vectorSearchOptions() { | ||
return VectorSearchConstructibleBson.EMPTY_IMMUTABLE; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.