Skip to content

Commit 5ad7a89

Browse files
author
Chris Cho
authored
DOCSP-33234: add vectorSearch builder (#459)
* DOCSP-33234: add vectorSearch builder
1 parent 4111533 commit 5ad7a89

File tree

2 files changed

+94
-14
lines changed

2 files changed

+94
-14
lines changed

source/fundamentals/builders/aggregates.txt

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ pipeline stage that returns literal documents from input values.
119119

120120
.. important::
121121

122-
If you use a ``$documents`` stage in an aggregation pipeline, it must be the first
122+
If you use a ``$documents`` stage in an aggregation pipeline, it must be the first
123123
stage in the pipeline.
124124

125-
The following example creates a pipeline stage that creates
126-
sample documents with a ``title`` field:
125+
The following example creates a pipeline stage that creates
126+
sample documents with a ``title`` field:
127127

128128
.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggBuilders.java
129129
:start-after: // begin documents
@@ -133,8 +133,8 @@ sample documents with a ``title`` field:
133133

134134
.. important::
135135

136-
If you use the ``documents()`` method to provide the input to an aggregation pipeline,
137-
you must call the ``aggregate()`` method on a database instead of on a
136+
If you use the ``documents()`` method to provide the input to an aggregation pipeline,
137+
you must call the ``aggregate()`` method on a database instead of on a
138138
collection.
139139

140140

@@ -975,3 +975,40 @@ aggregation stage:
975975
Learn more about this helper from the
976976
`searchMeta() API documentation <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#searchMeta(com.mongodb.client.model.search.SearchCollector)>`__.
977977

978+
Atlas Vector Search
979+
-------------------
980+
981+
.. important::
982+
983+
To learn what versions of MongoDB Atlas support this feature, see
984+
:atlas:`Limitations </atlas-vector-search/vector-search-stage/#limitations>`
985+
in the MongoDB Atlas documentation.
986+
987+
Use the ``vectorSearch()`` method to create a :atlas:`$vectorSearch </atlas-vector-search/vector-search-stage/>`
988+
pipeline stage that specifies a **semantic search**. A semantic search is
989+
a type of search which locates information that is similar in meaning.
990+
991+
To use this feature, you must set up a vector search index and index your
992+
vector embeddings. To learn how set these up in MongoDB Atlas, see
993+
:atlas:`How to Index Vector Embeddings for Vector Search </atlas-search/field-types/knn-vector/>`.
994+
995+
The following example shows how to build an aggregation pipeline that uses the
996+
``vectorSearch()`` and ``project()`` methods to compute a vector search score:
997+
998+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggBuilders.java
999+
:start-after: // begin vectorSearch
1000+
:end-before: // end vectorSearch
1001+
:language: java
1002+
:dedent:
1003+
1004+
The following example shows how you can print the score from the result of the
1005+
preceding aggregation pipeline:
1006+
1007+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggBuilders.java
1008+
:start-after: // begin vectorSearch-output
1009+
:end-before: // end vectorSearch-output
1010+
:language: java
1011+
:dedent:
1012+
1013+
Learn more about this helper from the
1014+
`vectorSearch() <{+api+}//apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#vectorSearch(com.mongodb.client.model.search.FieldSearchPath,java.lang.Iterable,java.lang.String,long,long)>`__ API documentation.

source/includes/fundamentals/code-snippets/builders/AggBuilders.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import static com.mongodb.client.model.Projections.*;
77
import static com.mongodb.client.model.Sorts.*;
88
import static com.mongodb.client.model.Accumulators.*;
9+
import static com.mongodb.client.model.search.SearchPath.fieldPath;
10+
import static com.mongodb.client.model.search.VectorSearchOptions.vectorSearchOptions;
911
import static java.util.Arrays.asList;
1012
// end static import
1113

@@ -16,8 +18,10 @@
1618
import com.mongodb.client.MongoCursor;
1719
import com.mongodb.client.MongoDatabase;
1820
import com.mongodb.client.model.*;
19-
21+
import com.mongodb.client.model.search.VectorSearchOptions;
2022
import java.util.List;
23+
import com.mongodb.client.model.search.FieldSearchPath;
24+
import org.bson.BsonString;
2125
import org.bson.Document;
2226
import org.bson.conversions.Bson;
2327

@@ -27,7 +31,7 @@ public class AggBuilders {
2731
private MongoDatabase database;
2832

2933
private AggBuilders() {
30-
final String uri = System.getenv("DRIVER_REF_SRV");
34+
final String uri = "<Your connection string>";
3135

3236
mongoClient = MongoClients.create(uri);
3337
database = mongoClient.getDatabase("sample_mflix");
@@ -77,7 +81,9 @@ public static void main(String[] args) {
7781
page.basicBucketAutoStage();
7882
page.bucketAutoOptionsStage();
7983
page.facetStage();
80-
page.setWindowFieldsStage();
84+
page.vectorSearchPipeline();
85+
// Fix required: https://jira.mongodb.org/browse/DOCSP-33327
86+
// page.setWindowFieldsStage();
8187
page.aggregationExample();
8288
}
8389

@@ -89,6 +95,7 @@ private void aggregationExample() {
8995
// end aggregationSample
9096
}
9197

98+
/* Fix required: https://jira.mongodb.org/browse/DOCSP-33327
9299
private void setWindowFieldsStage() {
93100
// begin setWindowFields
94101
Window pastMonth = Windows.timeRange(-1, MongoTimeUnit.MONTH, Windows.Bound.CURRENT);
@@ -97,6 +104,7 @@ private void setWindowFieldsStage() {
97104
WindowedComputations.avg("monthlyAvgTemp", "$temperature", pastMonth));
98105
// end setWindowFields
99106
}
107+
*/
100108

101109
private void facetStage() {
102110
// begin facet
@@ -247,12 +255,13 @@ private void advancedLookupStage() {
247255
new Document("$gte", asList("$instock", "$$order_qty")))))),
248256
project(fields(exclude("stock_item"), excludeId())));
249257

250-
List<Bson> innerJoinLookup = lookup("warehouses", variables, pipeline, "stockdata");
258+
Bson innerJoinLookup = lookup("warehouses", variables, pipeline, "stockdata");
251259
// end advanced lookup
252-
MongoCursor<Document> cursor = collection.aggregate(asList(advancedLookup)).cursor();
253-
cursor.forEachRemaining(doc -> System.out.println(doc.toJson()));
254-
database = mongoClient.getDatabase("sample_mflix");
255-
collection = database.getCollection("movies");
260+
// Missing advancedLookup assignment
261+
// MongoCursor<Document> cursor = collection.aggregate(asList(advancedLookup)).cursor();
262+
// cursor.forEachRemaining(doc -> System.out.println(doc.toJson()));
263+
// database = mongoClient.getDatabase("sample_mflix");
264+
// collection = database.getCollection("movies");
256265
}
257266

258267
private void basicLookupStage() {
@@ -303,7 +312,41 @@ private void matchStage() {
303312
// end match
304313
}
305314

306-
private void documents() {
315+
// Requires MongoDB version that supports $vectorSearch
316+
private void vectorSearchPipeline() {
317+
318+
// begin vectorSearch
319+
List<Double> queryVector = (asList(-0.0072121937, -0.030757688, -0.012945653));
320+
String indexName = "mflix_movies_embedding_index";
321+
FieldSearchPath fieldSearchPath = fieldPath("plot_embedding");
322+
int numCandidates = 2;
323+
int limit = 1;
324+
VectorSearchOptions options = vectorSearchOptions().filter(gte("year", 2016));
325+
326+
List<Bson> pipeline = asList(
327+
vectorSearch(
328+
fieldSearchPath,
329+
queryVector,
330+
indexName,
331+
numCandidates,
332+
limit,
333+
options),
334+
project(
335+
metaVectorSearchScore("vectorSearchScore")));
336+
// end vectorSearch
337+
338+
// begin vectorSearch-output
339+
Document found = collection.aggregate(pipeline).first();
340+
double score = found.getDouble("vectorSearchScore").doubleValue();
341+
342+
System.out.println("vectorSearch score: " + score);
343+
// end vectorSearch-output
344+
345+
// Example output: "vectorSearch score: 0.887437105178833"
346+
}
347+
348+
349+
private void documentsStage() {
307350
// begin documents
308351
documents(asList(
309352
new Document("title", "The Shawshank Redemption"),

0 commit comments

Comments
 (0)