Skip to content

Commit cc7449d

Browse files
committed
Review findings.
1 parent 6657508 commit cc7449d

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/VectorSearch.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353
public @interface VectorSearch {
5454

5555
/**
56-
* Configuration whether to use ANN or ENN for the search. ANN is the default.
56+
* Configuration whether to use
57+
* {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN} or
58+
* {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ENN} for the search.
5759
*
5860
* @return the search type to use.
5961
*/
60-
VectorSearchOperation.SearchType searchType() default VectorSearchOperation.SearchType.ENN;
62+
VectorSearchOperation.SearchType searchType() default VectorSearchOperation.SearchType.DEFAULT;
6163

6264
/**
6365
* Name of the Atlas Vector Search index to use. Atlas Vector Search doesn't return results if you misspell the index
@@ -98,21 +100,23 @@
98100
* specify {@link #numCandidates}. Limit accepts Value Expressions. A Vector Search method cannot define both,
99101
* {@code limit()} and a {@link org.springframework.data.domain.Limit} parameter. Supports Value Expressions.
100102
*
101-
* @return number of documents to return in the results
103+
* @return number of documents to return in the results.
102104
*/
103105
String limit() default "";
104106

105107
/**
106108
* Number of nearest neighbors to use during the search. Value must be less than or equal to ({@code <=})
107109
* {@code 10000}. You can't specify a number less than the {@link #limit() number of documents to return}. We
108110
* recommend that you specify a number at least {@code 20} times higher than the {@link #limit() number of documents
109-
* to return} to increase accuracy. This over-request pattern is the recommended way to trade off latency and recall
110-
* in your ANN searches, and we recommend tuning this parameter based on your specific dataset size and query
111-
* requirements. Required if the query uses
112-
* {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN}. Supports Value
113-
* Expressions.
111+
* to return} to increase accuracy.
112+
* <p>
113+
* This over-request pattern is the recommended way to trade off latency and recall in your ANN searches, and we
114+
* recommend tuning this parameter based on your specific dataset size and query requirements. Required if the query
115+
* uses
116+
* {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN}/{@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#DEFAULT}.
117+
* Supports Value Expressions.
114118
*
115-
* @return number of documents to return in the results
119+
* @return number of nearest neighbors to use during the search.
116120
*/
117121
String numCandidates() default "";
118122

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/VectorSearchDelegate.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
import org.bson.Document;
22+
import org.jspecify.annotations.Nullable;
2223

2324
import org.springframework.data.domain.Limit;
2425
import org.springframework.data.domain.Range;
@@ -46,7 +47,6 @@
4647
import org.springframework.data.repository.query.ValueExpressionDelegate;
4748
import org.springframework.data.repository.query.parser.Part;
4849
import org.springframework.data.repository.query.parser.PartTree;
49-
import org.springframework.lang.Nullable;
5050
import org.springframework.util.StringUtils;
5151

5252
/**
@@ -145,7 +145,8 @@ public QueryMetadata createQuery(ValueExpressionEvaluator evaluator, ResultProce
145145
numCandidates = ((Number) evaluator.evaluate(this.numCandidatesExpression)).intValue();
146146
} else if (this.numCandidates != null) {
147147
numCandidates = this.numCandidates;
148-
} else if (query.query().isLimited() && searchType == VectorSearchOperation.SearchType.ANN) {
148+
} else if (query.query().isLimited() && (searchType == VectorSearchOperation.SearchType.ANN
149+
|| searchType == VectorSearchOperation.SearchType.DEFAULT)) {
149150

150151
/*
151152
MongoDB: We recommend that you specify a number at least 20 times higher than the number of documents to return (limit) to increase accuracy.
@@ -195,7 +196,7 @@ ScoringFunction getSimilarityFunction(MongoParameterAccessor accessor) {
195196
* @param scoringFunction
196197
*/
197198
public record QueryMetadata(String path, String scoreField, Query query, VectorSearchOperation.SearchType searchType,
198-
Class<?> outputType, @org.jspecify.annotations.Nullable Integer numCandidates, ScoringFunction scoringFunction) {
199+
Class<?> outputType, @Nullable Integer numCandidates, ScoringFunction scoringFunction) {
199200

200201
/**
201202
* Create the Aggregation Pipeline.
@@ -210,12 +211,10 @@ public List<AggregationOperation> getAggregationPipeline(MongoQueryMethod queryM
210211
Vector vector = accessor.getVector();
211212
Score score = accessor.getScore();
212213
Range<Score> distance = accessor.getScoreRange();
213-
int limit;
214+
Limit limit = Limit.unlimited();
214215

215216
if (query.isLimited()) {
216-
limit = query.getLimit();
217-
} else {
218-
limit = Math.max(1, numCandidates() != null ? numCandidates() / 20 : 1);
217+
limit = Limit.of(query.getLimit());
219218
}
220219

221220
List<AggregationOperation> stages = new ArrayList<>();
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.lang.reflect.Method;
2222

23-
import org.jetbrains.annotations.NotNull;
2423
import org.junit.jupiter.api.Test;
2524

2625
import org.springframework.data.domain.Limit;
@@ -107,7 +106,6 @@ private MongoQueryMethod getMongoQueryMethod(Method method) {
107106
return new MongoQueryMethod(method, metadata, new SpelAwareProxyProjectionFactory(), converter.getMappingContext());
108107
}
109108

110-
@NotNull
111109
private static MongoParametersParameterAccessor getAccessor(MongoQueryMethod queryMethod, Object... values) {
112110
return new MongoParametersParameterAccessor(queryMethod, values);
113111
}

0 commit comments

Comments
 (0)