Docs Menu
Docs Home
/ / /
Java Sync Driver
/ / /

Atlas Vector Search

On this page

  • Overview
  • Perform a Vector Search
  • Vector Search Example
  • API Documentation

In this guide, you can learn how to use the Atlas 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 Atlas 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.

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

Back

Aggregation