Skip to content

Commit 437dea7

Browse files
Update documentation
1 parent 5592ec3 commit 437dea7

File tree

7 files changed

+154
-5
lines changed

7 files changed

+154
-5
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/VectorSearchOperation.java

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.bson.BinaryVector;
2727
import org.bson.Document;
28-
2928
import org.springframework.data.domain.Limit;
3029
import org.springframework.data.domain.Vector;
3130
import org.springframework.data.mongodb.core.mapping.MongoVector;
@@ -38,7 +37,6 @@
3837
/**
3938
* Performs a semantic search on data in your Atlas cluster. This stage is only available for Atlas Vector Search.
4039
* Vector data must be less than or equal to 4096 dimensions in width.
41-
* <p>
4240
* <h3>Limitations</h3> You cannot use this stage together with:
4341
* <ul>
4442
* <li>{@link org.springframework.data.mongodb.core.aggregation.LookupOperation Lookup} stages</li>

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/DefaultSearchIndexOperations.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public DefaultSearchIndexOperations(MongoOperations mongoOperations, String coll
6363
}
6464

6565
@Override
66-
public String ensureIndex(SearchIndexDefinition indexDefinition) {
66+
public String createIndex(SearchIndexDefinition indexDefinition) {
6767

6868
Document index = indexDefinition.getIndexDocument(entityTypeInformation,
6969
mongoOperations.getConverter().getMappingContext());

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperations.java

+14
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,23 @@ public interface IndexOperations {
3333
*
3434
* @param indexDefinition must not be {@literal null}.
3535
* @return the index name.
36+
* @deprecated in favor of {@link #createIndex(IndexDefinition)}.
3637
*/
38+
@Deprecated(since = "4.5", forRemoval = true)
3739
String ensureIndex(IndexDefinition indexDefinition);
3840

41+
/**
42+
* Create the index for the provided {@link IndexDefinition} exists for the collection indicated by the entity
43+
* class. If not it will be created.
44+
*
45+
* @param indexDefinition must not be {@literal null}.
46+
* @return the index name.
47+
* @since 4.5
48+
*/
49+
default String createIndex(IndexDefinition indexDefinition) {
50+
return ensureIndex(indexDefinition);
51+
}
52+
3953
/**
4054
* Alters the index with given {@literal name}.
4155
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/SearchIndexOperations.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ public interface SearchIndexOperations {
3333
* @param indexDefinition must not be {@literal null}.
3434
* @return the index name.
3535
*/
36-
String ensureIndex(SearchIndexDefinition indexDefinition);
36+
// TODO: keep or just go with createIndex?
37+
default String ensureIndex(SearchIndexDefinition indexDefinition) {
38+
return createIndex(indexDefinition);
39+
}
40+
41+
/**
42+
* Create the index for the given {@link SearchIndexDefinition} in the collection indicated by the entity class.
43+
*
44+
* @param indexDefinition must not be {@literal null}.
45+
* @return the index name.
46+
*/
47+
String createIndex(SearchIndexDefinition indexDefinition);
3748

3849
/**
3950
* Alters the search index matching the index {@link SearchIndexDefinition#getName() name}.
@@ -42,6 +53,7 @@ public interface SearchIndexOperations {
4253
*
4354
* @param indexDefinition the index definition.
4455
*/
56+
// TODO: keep or remove since it does not work reliably?
4557
void updateIndex(SearchIndexDefinition indexDefinition);
4658

4759
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoVector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.util.ObjectUtils;
2525

2626
/**
27-
* MongoDB-specific extension to {@link Vector} based on Mongo's {@link Binary}. Note that only float32 and int8
27+
* MongoDB-specific extension to {@link Vector} based on Mongo's {@link BinaryVector}. Note that only float32 and int8
2828
* variants can be represented as floating-point numbers. int1 returns an all-zero array for {@link #toFloatArray()} and
2929
* {@link #toDoubleArray()}.
3030
*

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

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
** xref:mongodb/change-streams.adoc[]
3434
** xref:mongodb/tailable-cursors.adoc[]
3535
** xref:mongodb/sharding.adoc[]
36+
** xref:mongodb/mongo-search-indexes.adoc[]
3637
** xref:mongodb/mongo-encryption.adoc[]
3738

3839
// Repository
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
[[mongo.search]]
2+
= MongoDB Search
3+
4+
MongoDB enables users to do keyword or lexical search as well as vector search data using dedicated search indexes.
5+
6+
[[mongo.search.vector]]
7+
== Vector Search
8+
9+
MongoDB Vector Search uses the `$vectorSearch` aggregation stage to run queries against specialized indexes.
10+
Please refer to the MongoDB documentation to learn more about requirements and restrictions of `vectorSearch` indexes.
11+
12+
[[mongo.search.vector.index]]
13+
=== Managing Vector Indexes
14+
15+
`SearchIndexOperationsProvider` implemented by `MongoTemplate` are the entrypoint to `SearchIndexOperations` offering various methods for managing vector indexes.
16+
17+
The following snippet shows how to create a vector index for a collection
18+
19+
.Create a Vector Index
20+
[tabs]
21+
======
22+
Java::
23+
+
24+
====
25+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
26+
----
27+
VectorIndex index = new VectorIndex("vector_index")
28+
.addVector("plotEmbedding"), vector -> vector.dimensions(1536).similarity(COSINE)) <1>
29+
.addFilter("year"); <2>
30+
31+
mongoTemplate.searchIndexOps(Movie.class) <3>
32+
.createIndex(index);
33+
----
34+
<1> A vector index may cover multiple vector embeddings that can be added via the `addVector` method.
35+
<2> Vector indexes can contain additional fields to narrow down search results when running queries.
36+
<3> Obtain `SearchIndexOperations` bound to the `Movie` type which is used for field name mapping.
37+
====
38+
39+
Mongo Shell::
40+
+
41+
====
42+
[source,console,indent=0,subs="verbatim,quotes",role="secondary"]
43+
----
44+
db.movie.createSearchIndex("movie", "vector_index",
45+
{
46+
"fields": [
47+
{
48+
"type": "vector",
49+
"numDimensions": 1536,
50+
"path": "plot_embedding", <1>
51+
"similarity": "cosine"
52+
},
53+
{
54+
"type": "filter",
55+
"path": "year"
56+
}
57+
]
58+
}
59+
)
60+
----
61+
<1> Field name `plotEmbedding` got mapped to `plot_embedding` considering a `@Field(name = "...")` annotation.
62+
====
63+
======
64+
65+
Once created, vector indexes are not immediately ready to use although the `exists` check returns `true`.
66+
The actual status of a search index can be obtained via `SearchIndexOperations#status(...)`.
67+
The `READY` state indicates the index is ready to accept queries.
68+
69+
[[mongo.search.vector.query]]
70+
=== Querying Vector Indexes
71+
72+
Vector indexes can be queried by issuing an aggregation using a `VectorSearchOperation` via `MongoOperations` as shown in the following example
73+
74+
.Query a Vector Index
75+
[tabs]
76+
======
77+
Java::
78+
+
79+
====
80+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
81+
----
82+
VectorSearchOperation search = VectorSearchOperation.search("vector_index") <1>
83+
.path("plotEmbedding") <2>
84+
.vector( ... )
85+
.numCandidates(150)
86+
.limit(10)
87+
.quantization(SCALAR)
88+
.withSearchScore("score"); <3>
89+
90+
AggregationResults<MovieWithSearchScore> results = mongoTemplate
91+
.aggregate(newAggregation(Movie.class, search), MovieWithSearchScore.class);
92+
----
93+
<1> Provide the name of the vector index to query since a collection may hold multiple ones.
94+
<2> The name of the path used for comparison.
95+
<3> Optionally add the search score with given name to the result document.
96+
====
97+
98+
Mongo Shell::
99+
+
100+
====
101+
[source,console,indent=0,subs="verbatim,quotes",role="secondary"]
102+
----
103+
db.embedded_movies.aggregate([
104+
{
105+
"$vectorSearch": {
106+
"index": "vector_index",
107+
"path": "plot_embedding", <1>
108+
"queryVector": [ ... ],
109+
"numCandidates": 150,
110+
"limit": 10,
111+
"quantization": "scalar"
112+
}
113+
},
114+
{
115+
"$addFields": {
116+
"score": { $meta: "vectorSearchScore" }
117+
}
118+
}
119+
])
120+
----
121+
<1> Field name `plotEmbedding` got mapped to `plot_embedding` considering a `@Field(name = "...")` annotation.
122+
====
123+
======
124+

0 commit comments

Comments
 (0)