Module spring.data.mongodb
Class VectorSearchOperation
java.lang.Object
org.springframework.data.mongodb.core.aggregation.VectorSearchOperation
- All Implemented Interfaces:
AggregationOperation
Performs a semantic search on data in your Atlas cluster. This stage is only available for Atlas Vector Search.
Vector data must be less than or equal to 4096 dimensions in width.
Limitations
You cannot use this stage together with:- Since:
- 4.5
- Author:
- Christoph Strobl, Mark Paluch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFluent API to configure a limit on the VectorSearchOperation builder.static interfaceFluent API to configure a path on the VectorSearchOperation builder.static interfaceInterface describing a query path contract.static classValue object capturing query paths.static enumSearch type, ANN as approximation or ENN for exact search.static classstatic interfaceFluent API to configure a vector on the VectorSearchOperation builder. -
Method Summary
Modifier and TypeMethodDescriptionfilter(org.bson.Document filter) Criteria expression that compares an indexed field with a boolean, date, objectId, number (not decimals), string, or UUID to use as a pre-filter.filter(CriteriaDefinition filter) Criteria expression that compares an indexed field with a boolean, date, objectId, number (not decimals), string, or UUID to use as a pre-filter.Return the MongoDB operator that is used for thisAggregationOperation.numCandidates(int numCandidates) Number of nearest neighbors to use during the search.Entrypoint to build aVectorSearchOperationstarting from theindexname to search.searchType(VectorSearchOperation.SearchType searchType) Configure the search type to use.org.bson.DocumenttoDocument(AggregationOperationContext context) List<org.bson.Document>withFilterBySore(Consumer<Criteria> score) Add aMatchOperationstage targeting the score field name.Add aAddFieldsOperationstage including the search score usingscoreas field name.withSearchScore(String scoreFieldName) Add aAddFieldsOperationstage including the search score usingscoreFieldNameas field name.
-
Method Details
-
search
Entrypoint to build aVectorSearchOperationstarting from theindexname to search. Atlas Vector Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster.- Parameters:
index- must not be null or empty.- Returns:
- new instance of
VectorSearchOperation.PathContributor.
-
searchType
@Contract("_ -> new") public VectorSearchOperation searchType(VectorSearchOperation.SearchType searchType) Configure the search type to use.VectorSearchOperation.SearchType.ENNleads to an exact search whileVectorSearchOperation.SearchType.ANNusesexact=false.- Parameters:
searchType- must not be null.- Returns:
- a new
VectorSearchOperationwithVectorSearchOperation.SearchTypeapplied.
-
filter
Criteria expression that compares an indexed field with a boolean, date, objectId, number (not decimals), string, or UUID to use as a pre-filter.Atlas Vector Search supports only the filters for the following MQL match expressions:
- $gt
- $lt
- $gte
- $lte
- $eq
- $ne
- $in
- $nin
- $nor
- $not
- $and
- $or
- Parameters:
filter- must not be null.- Returns:
- a new
VectorSearchOperationwithCriteriaDefinitionapplied.
-
filter
Criteria expression that compares an indexed field with a boolean, date, objectId, number (not decimals), string, or UUID to use as a pre-filter.Atlas Vector Search supports only the filters for the following MQL match expressions:
- $gt
- $lt
- $gte
- $lte
- $eq
- $ne
- $in
- $nin
- $nor
- $not
- $and
- $or
- Parameters:
filter- must not be null.- Returns:
- a new
VectorSearchOperationwithCriteriaDefinitionapplied.
-
numCandidates
Number of nearest neighbors to use during the search. Value must be less than or equal to (<=)10000. You can't specify a number less than the number of documents to return (limit). This field is required ifsearchType(SearchType)isVectorSearchOperation.SearchType.ANNorVectorSearchOperation.SearchType.DEFAULT.- Parameters:
numCandidates- number of nearest neighbors to use during the search- Returns:
- a new
VectorSearchOperationwithnumCandidatesapplied.
-
withSearchScore
Add aAddFieldsOperationstage including the search score usingscoreas field name.- Returns:
- a new
VectorSearchOperationwith search score applied. - See Also:
-
withSearchScore
Add aAddFieldsOperationstage including the search score usingscoreFieldNameas field name.- Parameters:
scoreFieldName- name of the score field.- Returns:
- a new
VectorSearchOperationwithscoreFieldNameapplied. - See Also:
-
withFilterBySore
Add aMatchOperationstage targeting the score field name. Implies that the score field is present by either reusing a previousAddFieldsOperationfromwithSearchScore()orwithSearchScore(String)or by adding a newAddFieldsOperationstage.- Returns:
- a new
VectorSearchOperationwith search score filter applied.
-
toDocument
Description copied from interface:AggregationOperation- Specified by:
toDocumentin interfaceAggregationOperation- Parameters:
context- theAggregationOperationContextto operate within. Must not be null.- Returns:
- the Document
-
toPipelineStages
Description copied from interface:AggregationOperationTurns theAggregationOperationinto list ofstagesby using the givenAggregationOperationContext. This allows a singleAggregationOptionsto add additional stages for eg.$sortor$limit.- Specified by:
toPipelineStagesin interfaceAggregationOperation- Parameters:
context- theAggregationOperationContextto operate within. Must not be null.- Returns:
- the pipeline stages to run through. Never null.
-
getOperator
Description copied from interface:AggregationOperationReturn the MongoDB operator that is used for thisAggregationOperation. Aggregation operations should implement this method to avoid document rendering.- Specified by:
getOperatorin interfaceAggregationOperation- Returns:
- the operator used for this
AggregationOperation.
-