Skip to content

Commit a2833e2

Browse files
committed
Documentation.
1 parent 642396c commit a2833e2

File tree

9 files changed

+127
-6
lines changed

9 files changed

+127
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@
7676
String path() default "";
7777

7878
/**
79-
* Takes a MongoDB JSON (MQL) string defining the pre-filter against indexed fields. Alias for
80-
* {@link VectorSearch#filter}.
79+
* Takes a MongoDB JSON (MQL) string defining the pre-filter against indexed fields. Supports Value Expressions. Alias
80+
* for {@link VectorSearch#filter}.
8181
*
8282
* @return an empty String by default.
8383
*/
8484
@AliasFor(annotation = Query.class)
8585
String value() default "";
8686

8787
/**
88-
* Takes a MongoDB JSON (MQL) string defining the pre-filter against indexed fields. Alias for
89-
* {@link VectorSearch#value}.
88+
* Takes a MongoDB JSON (MQL) string defining the pre-filter against indexed fields. Supports Value Expressions. Alias
89+
* for {@link VectorSearch#value}.
9090
*
9191
* @return an empty String by default.
9292
*/
@@ -96,7 +96,7 @@
9696
/**
9797
* Number of documents to return in the results. This value can't exceed the value of {@link #numCandidates} if you
9898
* specify {@link #numCandidates}. Limit accepts Value Expressions. A Vector Search method cannot define both,
99-
* {@code limit()} and a {@link org.springframework.data.domain.Limit} parameter.
99+
* {@code limit()} and a {@link org.springframework.data.domain.Limit} parameter. Supports Value Expressions.
100100
*
101101
* @return number of documents to return in the results
102102
*/
@@ -109,7 +109,8 @@
109109
* to return} to increase accuracy. This over-request pattern is the recommended way to trade off latency and recall
110110
* in your ANN searches, and we recommend tuning this parameter based on your specific dataset size and query
111111
* requirements. Required if the query uses
112-
* {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN}.
112+
* {@link org.springframework.data.mongodb.core.aggregation.VectorSearchOperation.SearchType#ANN}. Supports Value
113+
* Expressions.
113114
*
114115
* @return number of documents to return in the results
115116
*/

src/main/antora/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
** xref:repositories/create-instances.adoc[]
4646
** xref:repositories/query-methods-details.adoc[]
4747
** xref:mongodb/repositories/query-methods.adoc[]
48+
** xref:mongodb/repositories/vector-search.adoc[]
4849
** xref:mongodb/repositories/modifying-methods.adoc[]
4950
** xref:repositories/projections.adoc[]
5051
** xref:repositories/custom-implementations.adoc[]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:vector-search-intro-include: data-mongodb::partial$vector-search-intro-include.adoc
2+
:vector-search-model-include: data-mongodb::partial$vector-search-model-include.adoc
3+
:vector-search-repository-include: data-mongodb::partial$vector-search-repository-include.adoc
4+
:vector-search-scoring-include: data-mongodb::partial$vector-search-scoring-include.adoc
5+
:vector-search-method-derived-include: data-mongodb::partial$vector-search-method-derived-include.adoc
6+
:vector-search-method-annotated-include: data-mongodb::partial$vector-search-method-annotated-include.adoc
7+
8+
include::{commons}@data-commons::page$repositories/vector-search.adoc[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
To use Vector Search with MongoDB, you need a MongoDB Atlas instance that is either running in the cloud or by using https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-docker/[Docker].
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Annotated search methods use the `@VectorSearch` annotation to define parameters for the https://www.mongodb.com/docs/upcoming/reference/operator/aggregation/vectorSearch/[`$vectorSearch`] aggregation stage.
2+
3+
.Using `@VectorSearch` Search Methods
4+
====
5+
[source,java]
6+
----
7+
interface CommentRepository extends Repository<Comment, String> {
8+
9+
@VectorSearch(indexName = "cos-index", filter = "{country: ?0}")
10+
SearchResults<WithVector> searchAnnotatedByCountryAndEmbeddingWithin(String country, Vector embedding,
11+
Score distance);
12+
13+
@VectorSearch(indexName = "my-index", filter = "{country: ?0}", numCandidates = "#{#limit * 20}",
14+
searchType = VectorSearchOperation.SearchType.ANN)
15+
List<WithVector> findAnnotatedByCountryAndEmbeddingWithin(String country, Vector embedding, Score distance, int limit);
16+
}
17+
----
18+
====
19+
20+
Annotated Search Methods can define `filter` for pre-filter usage.
21+
22+
`filter`, `limit`, and `numCandidates` support xref:page$mongodb/value-expressions.adoc[Value Expressions] allowing references to search method arguments.
23+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MongoDB Search methods must use the `@VectorSearch` annotation to define the index name for the https://www.mongodb.com/docs/upcoming/reference/operator/aggregation/vectorSearch/[`$vectorSearch`] aggregation stage.
2+
3+
.Using `Near` and `Within` Keywords in Repository Search Methods
4+
====
5+
[source,java]
6+
----
7+
interface CommentRepository extends Repository<Comment, String> {
8+
9+
@VectorSearch(indexName = "my-index")
10+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, Score score);
11+
12+
@VectorSearch(indexName = "my-index")
13+
SearchResults<Comment> searchByEmbeddingWithin(Vector vector, Range<Similarity> range);
14+
15+
@VectorSearch(indexName = "my-index")
16+
SearchResults<Comment> searchByCountryAndEmbeddingWithin(String country, Vector vector, Range<Similarity> range);
17+
}
18+
----
19+
====
20+
21+
Derived Search Methods can define domain model attributes to create the pre-filter for indexed fields.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
====
2+
[source,java]
3+
----
4+
class Comment {
5+
6+
@Id String id;
7+
String country;
8+
String comment;
9+
10+
Vector embedding;
11+
12+
// getters, setters, …
13+
}
14+
----
15+
====
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.Using `SearchResult<T>` in a Repository Search Method
2+
====
3+
[source,java]
4+
----
5+
interface CommentRepository extends Repository<Comment, String> {
6+
7+
@VectorSearch(indexName = "my-index")
8+
SearchResults<Comment> searchByCountryAndEmbeddingNear(String country, Vector vector, Score score,
9+
Limit limit);
10+
11+
@VectorSearch(indexName = "my-index")
12+
SearchResults<WithVector> searchAnnotatedByCountryAndEmbeddingWithin(String country, Vector embedding,
13+
Score score);
14+
15+
}
16+
17+
SearchResults<Comment> results = repository.searchByCountryAndEmbeddingNear("en", Vector.of(…), Score.of(0.9), Limit.of(10));
18+
----
19+
====
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
MongoDB reports the score directly as similarity value.
2+
The scoring function must be specified in the index and therefore, Vector search methods do not consider the `Score.scoringFunction`.
3+
The scoring function defaults to `ScoringFunction.unspecified()` as there is no information inside of search results how the score has been computed.
4+
5+
.Using `Score` and `Similarity` in a Repository Search Methods
6+
====
7+
[source,java]
8+
----
9+
interface CommentRepository extends Repository<Comment, String> {
10+
11+
@VectorSearch(…)
12+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, Score similarity);
13+
14+
@VectorSearch(…)
15+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, Similarity similarity);
16+
17+
@VectorSearch(…)
18+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, Range<Similarity> range);
19+
}
20+
21+
repository.searchByEmbeddingNear(Vector.of(…), Score.of(0.9)); <1>
22+
23+
repository.searchByEmbeddingNear(Vector.of(…), Similarity.of(0.9)); <2>
24+
25+
repository.searchByEmbeddingNear(Vector.of(…), Similarity.between(0.5, 1)); <3>
26+
----
27+
28+
<1> Run a search and return results with a similarity of `0.9` or greater.
29+
<2> Return results with a similarity of `0.9` or greater.
30+
<3> Return results with a similarity of between `0.5` and `1.0` or greater.
31+
====
32+

0 commit comments

Comments
 (0)