Docs Menu
Docs Home
/ /

MongoDB Vector Search

In this guide, you can learn how to use the MongoDB Vector Search feature in the Java driver. The Aggregates builders class provides the the vectorSearch() helper method that you can use to create a $vectorSearch pipeline stage. This pipeline stage allows you to perform a semantic search on your documents. A semantic search is a type of search which locates information that is similar in meaning, but not necessarily identical, to your provided search term or phrase.

Important

Feature Compatibility

To learn what versions of MongoDB Atlas support this feature, see Limitations in the MongoDB Atlas documentation.

To use this feature, you must create a vector search index and index your vector embeddings. To learn about how to programmatically create a vector search index, see the MongoDB Search and Vector Search Indexes section in the Indexes guide. To learn more about vector embeddings, see How to Index Vector Embeddings for Vector Search in the Atlas documentation.

After you create a vector search index on your vector embeddings, you can reference this index in your pipeline stage, as shown in the following section.

The following example shows how to build an aggregation pipeline that uses the vectorSearch() and project() methods to compute a vector search score:

// Create an instance of the BinaryVector class as the query vector
BinaryVector queryVector = BinaryVector.floatVector(
new float[]{0.0001f, 1.12345f, 2.23456f, 3.34567f, 4.45678f});
// Specify the index name for the vector embedding index
String indexName = "mflix_movies_embedding_index";
// Specify the path of the field to search on
FieldSearchPath fieldSearchPath = fieldPath("plot_embedding");
// Limit the number of matches to 1
int limit = 1;
// Create a pre-filter to only search within a subset of documents
VectorSearchOptions options = exactVectorSearchOptions()
.filter(gte("year", 2016));
// Create the vectorSearch pipeline stage
List<Bson> pipeline = asList(
vectorSearch(
fieldSearchPath,
queryVector,
indexName,
limit,
options),
project(
metaVectorSearchScore("vectorSearchScore")));

Tip

Query Vector Type

The preceding example creates an instance of BinaryVector to serve as the query vector, but you can also create a List of Double instances. However, we recommend that you use the BinaryVector type to improve storage efficiency.

The following example shows how you can run the aggregation and print the vector search meta-score from the result of the preceding aggregation pipeline:

Document found = collection.aggregate(pipeline).first();
double score = found.getDouble("vectorSearchScore").doubleValue();
System.out.println("vectorSearch score: " + score);

Tip

Java Driver Vector Search Examples

Visit the Atlas documentation to find more tutorials on using the Java driver to perform Atlas Vector Searches.

Note

This feature is only available in the Java Sync driver v5.7 or later. To learn more about how to install and use the latest version of the driver, see the mongodb-driver-sync package on Maven Central.

You can automate vector generation for text searches by querying an auto-embedding MongoDB Vector Search index. To learn about how to create an auto-embedding index, see MongoDB Auto-Embedding Search Index Model.

The following example constructs a vector search query that searches for semantic similarity to the phrase time travel in the plot field. The query uses an auto-embedding MongoDB Vector Search index on the plot field named auto_embedding_index:

List<Bson> pipeline = asList(
vectorSearch(
fieldPath("plot"),
textQuery("time travel"),
"auto_embedding_index",
10L,
approximateVectorSearchOptions(150L)
),
project(
fields(include("title", "plot"), excludeId())
)
);
List<Document> results = collection.aggregate(pipeline).into(new ArrayList<>());
for (Document doc : results) {
System.out.println("Title: " + doc.getString("title"));
System.out.println("Plot: " + doc.getString("plot"));
System.out.println("---");
}
Title: Manuel on the Island of Wonders
Plot: Manuel's fantasy travel through Time goes from Long Ago (Episode 1 - O jardim proibido / Le Jardin interdit) through Now (Episode 2 - O pique-nique dos sonhos / Le Pique-nique des rèves), ...
---
Title: 11 Minutes Ago
Plot: Traveling in 11-minute increments, a time-tumbler from 48-years in the future spends two years of his life weaving through a two-hour wedding reception.
---
Title: Time Freak
Plot: A neurotic inventor creates a time machine and gets lost traveling around yesterday.
---
Title: Timecrimes
Plot: A man accidentally gets into a time machine and travels back in time nearly an hour. Finding himself will be the first of a series of disasters of unforeseeable consequences.
---
Title: The Little Girl Who Conquered Time
Plot: A high-school girl acquires the ability to time travel.
---
Title: Time Traveller
Plot: A high-school girl acquires the ability to time travel.
---
Title: Je t'aime je t'aime
Plot: Recovering from an attempted suicide, a man is selected to participate in a time travel experiment that has only been tested on mice. A malfunction in the experiment causes the man to ...
---
Title: A.P.E.X.
Plot: A time-travel experiment in which a robot probe is sent from the year 2073 to the year 1973 goes terribly wrong thrusting one of the project scientists, a man named Nicholas Sinclair into a...
---
Title: The Ah of Life
Plot: Theoretical mathematician, Nigel Kline finds himself the subject of his own vertical time study. Entering into Einstein's relativity, three versions of Nigel face off with each other, weaving time and space in a world of fluid moments.
---
Title: About Time
Plot: At the age of 21, Tim discovers he can travel in time and change what happens and has happened in his own life. His decision to make his world a better place by getting a girlfriend turns out not to be as easy as you might think.
---

Note

When using an auto embedding index, directly provide the text to search rather than a vector representation of that text.

For more information about auto-embedding MongoDB Vector Search indexes, see the MongoDB Auto-Embedding Search Index Model section of the Indexes guide.

To learn more about the methods and types mentioned in this guide, see the following API documentation:

Back

MongoDB Search

On this page