How to Index Fields for Vector Search
On this page
- Considerations
- Supported Clients
- Syntax
- Atlas Vector Search Index Fields
- About the
vector
Type - About the
filter
Type - Create an Atlas Vector Search Index
- Prerequisites
- Required Access
- Index Limitations
- Procedure
- View an Atlas Vector Search Index
- Required Access
- Procedure
- Edit an Atlas Vector Search Index
- Required Access
- Procedure
- Delete an Atlas Vector Search Index
- Required Access
- Procedure
- Index Status
You can use the vectorSearch
type to index fields for running
$vectorSearch
queries. You can define the index for the
vector embeddings that you want to query and the boolean, date, objectId,
numeric, string, or UUID values that you want to use to pre-filter your
data. Filtering your data is useful to narrow the scope of your semantic
search and ensure that certain vector embeddings are not considered for
comparison, such as in a multi-tenant environment.
You can use the Atlas UI, Atlas Administration API,
Atlas CLI, mongosh
, or a supported MongoDB Driver
to create your Atlas Vector Search index.
Note
You can't use the deprecated knnBeta operator to query
fields indexed using the vectorSearch
type index definition.
Considerations
In a vectorSearch
type index definition, you can index arrays with
only a single element. You can't index fields inside arrays of documents
or fields inside arrays of objects. You can index fields inside
documents using the dot notation.
Before indexing your embeddings, we recommend converting your embeddings
to BSON BinData vector
subtype float32
, int1
, or int8
vectors for efficient storage
in your Atlas cluster. To learn more, see how to convert
your embeddings to BSON vectors.
Note
Atlas Vector Search support for the following is available as a Preview feature:
Ingestion of BSON BinData
vector
subtypeint1
.Automatic scalar quantization.
Automatic binary quantization.
When you use Atlas Vector Search indexes, you might experience elevated resource consumption on an idle node for your Atlas cluster. This is due to the underlying mongot process, which performs various essential operations for Atlas Vector Search. The CPU utilization on an idle node can vary depending on the number, complexity, and size of the indexes.
Supported Clients
You can create and manage Atlas Vector Search indexes through the Atlas UI,
mongosh
, Atlas CLI, Atlas Administration API, and the following
MongoDB Drivers:
Syntax
The following syntax defines the vectorSearch
index type:
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 "path": "<field-to-index>", 6 "numDimensions": <number-of-dimensions>, 7 "similarity": "euclidean | cosine | dotProduct", 8 "quantization": "none | scalar | binary" 9 }, 10 { 11 "type": "filter", 12 "path": "<field-to-index>" 13 }, 14 ... 15 ] 16 }
Atlas Vector Search Index Fields
The Atlas Vector Search index definition takes the following fields:
Option | Type | Necessity | Purpose |
---|---|---|---|
| array of documents | Required | Vector and filter fields to index, one per document. At least one document must contain the field definition for the vector field. You can optionally also index boolean, date, number, objectId, string, and UUID fields, one per document, for pre-filtering the data. |
| string | Required | Field type to use to index fields for
|
| string | Required | Name of the field to index. For nested fields, use dot notation to specify path to embedded fields. You cannot index field names with two consecutive dots or field names
ending with dots. For example, Atlas Vector Search doesn't support indexing the
following field names: |
| int | Required | Number of vector dimensions that Atlas Vector Search enforces at index-time and
query-time. You must specify a value less than or equal to
You can set this field only for |
| string | Required | Vector similarity function to use to search for top K-nearest
neighbors. You can set this field only for
To learn more, see About the Similarity Functions. |
| string | Optional | Type of automatic vector quantization for your vectors. You can
specify the type of quantization to apply on your vectors. Use
this setting only if your embeddings are
If precision is critical, select To learn more, see Vector Quantization. |
About the vector
Type
Your index definition's vector
field must contain an array of numbers of
one of the following types:
BSON
double
BSON BinData
vector
subtypefloat32
BSON BinData
vector
subtypeint1
BSON BinData
vector
subtypeint8
Note
To learn more about generating BSON BinData vector
subtype float32
or vector
subtype int1
or int8
vectors for your data, see
How to Ingest Pre-Quantized Vectors.
Atlas Vector Search support for the following is available as a Preview feature:
Ingestion of BSON BinData
vector
subtypeint1
.Automatic scalar quantization.
Automatic binary quantization.
You must index the vector field as the vector
type inside the
fields
array.
The following syntax defines the vector
field type:
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 "path": <field-to-index>, 6 "numDimensions": <number-of-dimensions>, 7 "similarity": "euclidean | cosine | dotProduct", 8 "quantization": "none | scalar | binary" 9 }, 10 ... 11 ] 12 }
About the Similarity Functions
Atlas Vector Search supports the following similarity functions:
euclidean
- measures the distance between ends of vectors. This value allows you to measure similarity based on varying dimensions. To learn more, see Euclidean.cosine
- measures similarity based on the angle between vectors. This value allows you to measure similarity that isn't scaled by magnitude. You can't use zero magnitude vectors withcosine
. To measure cosine similarity, we recommend that you normalize your vectors and usedotProduct
instead.dotProduct
- measures similarity likecosine
, but takes into account the magnitude of the vector. If you normalize the magnitude,cosine
anddotProduct
are almost identical in measuring similarity.To use
dotProduct
, you must normalize the vector to unit length at index-time and query-time.
The following table shows the similarity functions for the various types:
Vector Embeddings Type | euclidean | cosine | dotProduct |
---|---|---|---|
| √ | ||
| √ | √ | √ |
| √ | √ | √ |
| √ | √ | √ |
For vector ingestion.
For automatic scalar or binary quantization.
For best performance, check your embedding model to determine which
similarity function aligns with your embedding model's training
process. If you don't have any guidance, start with dotProduct
.
Setting fields.similarity
to the dotProduct
value allows you
to efficiently measure similarity based on both angle and magnitude.
dotProduct
consumes less computational resources than cosine
and is efficient when vectors are of unit length. However, if your
vectors aren't normalized, evaluate the similarity scores in the
results of a sample query for euclidean
distance and cosine
similarity to determine which corresponds to reasonable results.
About the filter
Type
You can optionally index boolean, date, number, objectId, string, and UUID fields to pre-filter your data. Filtering your data is useful to narrow the scope of your semantic search and ensure that not all vectors are considered for comparison. It reduces the number of documents against which to run similarity comparisons, which can decrease query latency and increase the accuracy of search results.
You must index the fields that you want to filter by using the
filter
type inside the fields
array.
The following syntax defines the filter
field type:
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 ... 6 }, 7 { 8 "type": "filter", 9 "path": "<field-to-index>" 10 }, 11 ... 12 ] 13 }
Note
Pre-filtering your data doesn't affect the score that Atlas Vector Search returns
using $vectorSearchScore
for $vectorSearch
queries.
Create an Atlas Vector Search Index
An Atlas Search index is a data structure that categorizes data in an easily searchable format. It is a mapping between terms and the documents that contain those terms. Atlas Search indexes enable faster retrieval of documents using certain identifiers. You must configure an Atlas Search index to query data in your Atlas cluster using Atlas Search.
You can create an Atlas Search index on a single field or on multiple fields. We recommend that you index the fields that you regularly use to sort or filter your data in order to quickly retrieve the documents that contain the relevant data at query-time.
You can create an Atlas Vector Search index for all collections that contain vector
embeddings less than or equal to 4096 dimensions in length for any kind
of data along with other data on your Atlas cluster through the
Atlas UI, Atlas Administration API, Atlas CLI, mongosh
,
or a supported MongoDB Driver.
Prerequisites
To create an Atlas Vector Search index, you must have an Atlas cluster with the following prerequisites:
MongoDB version
6.0.11
,7.0.2
, or higherA collection for which to create the Atlas Vector Search index
Note
You can use the mongosh
command or driver helper methods to create
Atlas Search indexes on all Atlas cluster tiers. For a list of supported driver
versions, see Supported Clients.
Required Access
You need the Project Data Access Admin
or higher role to create
and manage Atlas Vector Search indexes.
Index Limitations
You cannot create more than:
3 indexes on
M0
clusters.5 indexes on
M2
clusters.10 indexes on
M5
clusters.10 indexes on Flex clusters.
We recommend that you create no more than 2,500 search indexes on a
single M10+
cluster.
Procedure
➤ Use the Select your language drop-down menu to select the client you want to use to create your index.
Note
The procedure includes index definition examples for the
embedded_movies
collection in the sample_mflix
database. If
you load the sample data on your
cluster and create the example Atlas Search indexes for this collection,
you can run the sample $vectorSearch
queries against this
collection. To learn more about the sample queries that you can run,
see $vectorSearch Examples.
To create an Atlas Vector Search index for a collection using the
Atlas Administration API, send a POST
request to the Atlas Search
indexes
endpoint with the required parameters.
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --include \ 5 --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ 6 --data ' 7 { 8 "database": "<name-of-database>", 9 "collectionName": "<name-of-collection>", 10 "type": "vectorSearch", 11 "name": "<index-name>", 12 "definition": { 13 "fields":[ 14 { 15 "type": "vector", 16 "path": <field-to-index>, 17 "numDimensions": <number-of-dimensions>, 18 "similarity": "euclidean | cosine | dotProduct" 19 }, 20 { 21 "type": "filter", 22 "path": "<field-to-index>" 23 }, 24 ... 25 } 26 ] 27 }'
To learn more about the syntax and parameters for the endpoint, see Create One Atlas Search Index.
Example
The following index definition indexes the plot_embedding
field as the vector
type and the genres
and year
fields as the filter
type in an Atlas Vector Search index. The
plot_embedding
field contains embeddings created using
OpenAI's text-embedding-ada-002
embeddings model. The index
definition specifies 1536
vector dimensions and measures
similarity using dotProduct
function.
The following index definition indexes only the vector embeddings field for performing vector search.
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --include \ 5 --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ 6 --data ' 7 { 8 "database": "sample_mflix", 9 "collectionName": "embedded_movies", 10 "type": "vectorSearch", 11 "name": "vector_index", 12 "definition: { 13 "fields":[ 14 { 15 "type": "vector", 16 "path": "plot_embedding", 17 "numDimensions": 1536, 18 "similarity": "dotProduct" 19 } 20 ] 21 } 22 }'
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --include \ 5 --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ 6 --data ' 7 { 8 "database": "sample_mflix", 9 "collectionName": "embedded_movies", 10 "type": "vectorSearch", 11 "name": "vector_index", 12 "definition: { 13 "fields":[ 14 { 15 "type": "vector", 16 "path": "plot_embedding", 17 "numDimensions": 1536, 18 "similarity": "dotProduct" 19 }, 20 { 21 "type": "filter", 22 "path": "genres" 23 }, 24 { 25 "type": "filter", 26 "path": "year" 27 } 28 ] 29 } 30 }'
To create an Atlas Vector Search index for a collection using the Atlas CLI v1.14.3 or later, perform the following steps:
Create a .json
file and define the index in the file.
Your index definition should resemble the following format:
1 { 2 "database": "<name-of-database>", 3 "collectionName": "<name-of-collection>", 4 "type": "vectorSearch", 5 "name": "<index-name>", 6 "fields":[ 7 { 8 "type": "vector", 9 "path": "<field-to-index>", 10 "numDimensions": <number-of-dimensions>, 11 "similarity": "euclidean | cosine | dotProduct" 12 }, 13 { 14 "type": "filter", 15 "path": "<field-to-index>" 16 }, 17 ... 18 ] 19 }
Example
Create a file named vector-index.json
.
Replace the following placeholder values and save the file.
| Database that contains the collection for which you want to create the index. |
| Collection for which you want to create the index. |
| Name of your index. If you omit the index name, Atlas Vector Search names the index |
| Number of vector dimensions that Atlas Vector Search enforces at index-time and query-time. |
| Vector and filter fields to index. |
Example
Copy and paste the following index definition into the
vector-index.json
file. The following index definition
indexes the plot_embedding
field as the vector
type
and the genres
and year
fields as the filter
type
in an Atlas Vector Search index. The plot_embedding
field contains
embeddings created using OpenAI's text-embedding-ada-002
embeddings model. The index definition specifies 1536
vector dimensions and measures similarity using dotProduct
function.
The following index definition indexes only the vector embeddings field for performing vector search.
1 { 2 "database": "sample_mflix", 3 "collectionName": "embedded_movies", 4 "type": "vectorSearch", 5 "name": "vector_index", 6 "fields": [ 7 { 8 "type": "vector", 9 "path": "plot_embedding", 10 "numDimensions": 1536, 11 "similarity": "dotProduct" 12 } 13 ] 14 }
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 { 2 "database": "sample_mflix", 3 "collectionName": "embedded_movies", 4 "type": "vectorSearch", 5 "name": "vector_index", 6 "fields":[ 7 { 8 "type": "vector", 9 "path": "plot_embedding", 10 "numDimensions": 1536, 11 "similarity": "dotProduct" 12 }, 13 { 14 "type": "filter", 15 "path": "genres" 16 }, 17 { 18 "type": "filter", 19 "path": "year" 20 } 21 ] 22 }
Run the following command to create the index.
atlas clusters search indexes create --clusterName [cluster_name] --file [vector_index].json
In the command, replace the following placeholder values:
cluster_name
is the name of the Atlas cluster that contains the collection for which you want to create the index.vector_index
is the name of the JSON file that contains the index definition for the Atlas Vector Search index.
Example
atlas clusters search indexes create --clusterName [cluster_name] --file vector-index.json
To learn more about the command syntax and parameters, see the Atlas CLI documentation for the atlas clusters search indexes create command.
In Atlas, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter a name for the index.
Index name must be unique within the namespace, regardless of the index type.
Example
Enter vector_index as the name for the example index. If you already have an index named vector_index on this collection, enter a different name for the index.
In the Database and Collection section, find the database, and select the collection for which to create the index.
Example
In the Database and Collection section, find the
sample_mflix
database, and select theembedded_movies
collection.
Specify an index definition.
An Atlas Vector Search index resembles the following example:
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 "path": <field-to-index>, 6 "numDimensions": <number-of-dimensions>, 7 "similarity": "euclidean | cosine | dotProduct" 8 }, 9 { 10 "type": "filter", 11 "path": "<field-to-index>" 12 }, 13 ... 14 ] 15 }
To learn more about the fields in the index, see How to Index Fields for Vector Search.
Example
The following index definition indexes the plot_embedding
field as the vector
type and the genres
and year
fields as the filter
type in an Atlas Vector Search index. The
plot_embedding
field contains embeddings created using
OpenAI's text-embedding-ada-002
embeddings model. The
index definition specifies 1536
vector dimensions and
measures similarity using dotProduct
function.
The following index definition indexes only the vector embeddings field for performing vector search.
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 }
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }, 8 { 9 "type": "filter", 10 "path": "genres" 11 }, 12 { 13 "type": "filter", 14 "path": "year" 15 }] 16 }
Check the status.
The newly created index appears on the Atlas Search tab. While the index is building, the Status field reads Build in Progress. When the index is finished building, the Status field reads Active.
Note
Larger collections take longer to index. You will receive an email notification when your index is finished building.
To create an Atlas Vector Search index for a collection using mongosh
v2.1.2 or later, perform the following steps:
Connect to the Atlas cluster using mongosh
.
To learn more, see Connect via mongosh
.
Run the db.collection.createSearchIndex()
method.
The db.collection.createSearchIndex()
method has the
following syntax:
1 db.<collectionName>.createSearchIndex( 2 "<index-name>", 3 "vectorSearch", //index type 4 { 5 fields: [ 6 { 7 "type": "vector", 8 "numDimensions": <number-of-dimensions>, 9 "path": "<field-to-index>", 10 "similarity": "euclidean | cosine | dotProduct" 11 }, 12 { 13 "type": "filter", 14 "path": "<field-to-index>" 15 }, 16 ... 17 ] 18 } 19 );
Example
The following index definition indexes the plot_embedding
field as the vector
type and the genres
and year
fields as the filter
type in an Atlas Vector Search index. The
plot_embedding
field contains embeddings created using
OpenAI's text-embedding-ada-002
embeddings model. The
index definition specifies 1536
vector dimensions and
measures similarity using dotProduct
function.
The following index definition indexes only the vector embeddings field for performing vector search.
1 db.embedded_movies.createSearchIndex( 2 "vector_index", 3 "vectorSearch", 4 { 5 "fields": [ 6 { 7 "type": "vector", 8 "path": "plot_embedding", 9 "numDimensions": 1536, 10 "similarity": "dotProduct", 11 "quantization": "scalar" 12 } 13 ] 14 } 15 );
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 db.embedded_movies.createSearchIndex( 2 "vector_index", 3 "vectorSearch", 4 { 5 "fields": [ 6 { 7 "type": "vector", 8 "path": "plot_embedding", 9 "numDimensions": 1536, 10 "similarity": "dotProduct" 11 }, 12 { 13 "type": "filter", 14 "path": "genres" 15 }, 16 { 17 "type": "filter", 18 "path": "year" 19 } 20 ] 21 } 22 );
To create an Atlas Vector Search index for a collection using the C# driver v3.1.0 or later, perform the following steps:
Create a .cs
file and define the index in the file.
1 using MongoDB.Bson; 2 using MongoDB.Driver; 3 using System; 4 using System.Threading; 5 6 // Connect to your Atlas deployment 7 private const string MongoConnectionString = "<connectionString>"; 8 var client = new MongoClient(MongoConnectionString); 9 10 // Access your database and collection 11 var database = client.GetDatabase("<databaseName>"); 12 var collection = database.GetCollection<BsonDocument>("<collectionName>"); 13 14 // Create your index model, then create the search index 15 var name = "<indexName>"; 16 var type = SearchIndexType.VectorSearch; 17 18 var definition = new BsonDocument 19 { 20 { "fields", new BsonArray 21 { 22 new BsonDocument 23 { 24 { "type", "vector" }, 25 { "path", "<fieldToIndex>" }, 26 { "numDimensions", <numberOfDimensions> }, 27 { "similarity", "euclidean | cosine | dotProduct" } 28 } 29 } 30 } 31 }; 32 33 var model = new CreateSearchIndexModel(name, type, definition); 34 35 var searchIndexView = collection.SearchIndexes; 36 searchIndexView.CreateOne(model); 37 Console.WriteLine($"New search index named {name} is building."); 38 39 // Wait for initial sync to complete 40 Console.WriteLine("Polling to check if the index is ready. This may take up to a minute."); 41 bool queryable = false; 42 while (!queryable) 43 { 44 var indexes = searchIndexView.List(); 45 foreach (var index in indexes.ToEnumerable()) 46 { 47 if (index["name"] == name) 48 { 49 queryable = index["queryable"].AsBoolean; 50 } 51 } 52 if (!queryable) 53 { 54 Thread.Sleep(5000); 55 } 56 } 57 Console.WriteLine($"{name} is ready for querying.");
1 using MongoDB.Bson; 2 using MongoDB.Driver; 3 using System; 4 using System.Threading; 5 6 // Connect to your Atlas deployment 7 private const string MongoConnectionString = "<connectionString>"; 8 var client = new MongoClient(MongoConnectionString); 9 10 // Access your database and collection 11 var database = client.GetDatabase("<databaseName>"); 12 var collection = database.GetCollection<BsonDocument>("<collectionName>"); 13 14 // Create your index models and add them to an array 15 var type = SearchIndexType.VectorSearch; 16 17 var definitionOne = new BsonDocument 18 { 19 { "fields", new BsonArray 20 { 21 new BsonDocument 22 { 23 { "type", "vector" }, 24 { "path", "<fieldToIndex>" }, 25 { "numDimensions", <numberOfDimensions> }, 26 { "similarity", "euclidean | cosine | dotProduct" } 27 } 28 } 29 } 30 }; 31 var modelOne = new CreateSearchIndexModel("<indexName>", type, definitionOne); 32 33 var definitionTwo = new BsonDocument 34 { 35 { "fields", new BsonArray 36 { 37 new BsonDocument 38 { 39 { "type", "vector" }, 40 { "path", "<fieldToIndex>" }, 41 { "numDimensions", <numberOfDimensions> }, 42 { "similarity", "euclidean | cosine | dotProduct" } 43 } 44 } 45 } 46 }; 47 var modelTwo = new CreateSearchIndexModel("<indexName>", type, definitionTwo); 48 49 var models = new CreateSearchIndexModel[] { modelOne, modelTwo }; 50 51 // Create the search indexes 52 var searchIndexView = collection.SearchIndexes; 53 searchIndexView.CreateMany(models); 54 55 Console.WriteLine($"New search indexes are building. This may take up to a minute.");
Example
Create a file named IndexService.cs
.
Replace the following values and save the file.
| Atlas connection string. To learn more, see Connect via Drivers. |
| Database that contains the collection for which you want to create the index. |
| Collection for which you want to create the index. |
| Name of your index. If you omit the index name, Atlas Search names the index |
| Number of vector dimensions that Atlas Vector Search enforces at index-time and query-time. |
| Vector and filter fields to index. |
Example
Copy and paste the following into the IndexService.cs
and
replace the <connectionString>
placeholder value. The following index
definition indexes the plot_embedding
field as the
vector
type and the genres
and year
fields as the
filter
type in an Atlas Vector Search index. The plot_embedding
field contains embeddings created using OpenAI's
text-embedding-ada-002
embeddings model. The index
definition specifies 1536
vector dimensions and measures
similarity using dotProduct
function.
The following index definition indexes only the vector
embeddings field (plot_embedding
) for performing
vector search.
1 namespace query_quick_start; 2 3 using MongoDB.Bson; 4 using MongoDB.Driver; 5 using System; 6 using System.Threading; 7 8 public class IndexService 9 { 10 // Replace the placeholder with your Atlas connection string 11 private const string MongoConnectionString = "<connection-string>"; 12 public void CreateVectorIndex() 13 { 14 try 15 { 16 // Connect to your Atlas cluster 17 var client = new MongoClient(MongoConnectionString); 18 var database = client.GetDatabase("sample_mflix"); 19 var collection = database.GetCollection<BsonDocument>("embedded_movies"); 20 21 var searchIndexView = collection.SearchIndexes; 22 var name = "vector_index"; 23 var type = SearchIndexType.VectorSearch; 24 25 var definition = new BsonDocument 26 { 27 { "fields", new BsonArray 28 { 29 new BsonDocument 30 { 31 { "type", "vector" }, 32 { "path", "plot_embedding" }, 33 { "numDimensions", 1536 }, 34 { "similarity", "dotProduct" }, 35 { "quantization", "scalar" } 36 } 37 } 38 } 39 }; 40 41 var model = new CreateSearchIndexModel(name, type, definition); 42 43 searchIndexView.CreateOne(model); 44 Console.WriteLine($"New search index named {name} is building."); 45 46 // Polling for index status 47 Console.WriteLine("Polling to check if the index is ready. This may take up to a minute."); 48 bool queryable = false; 49 while (!queryable) 50 { 51 var indexes = searchIndexView.List(); 52 foreach (var index in indexes.ToEnumerable()) 53 { 54 if (index["name"] == name) 55 { 56 queryable = index["queryable"].AsBoolean; 57 } 58 } 59 if (!queryable) 60 { 61 Thread.Sleep(5000); 62 } 63 } 64 Console.WriteLine($"{name} is ready for querying."); 65 } 66 catch (Exception e) 67 { 68 Console.WriteLine($"Exception: {e.Message}"); 69 } 70 } 71 }
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 namespace query_quick_start; 2 3 using MongoDB.Bson; 4 using MongoDB.Driver; 5 using System; 6 using System.Threading; 7 8 public class IndexService 9 { 10 // Replace the placeholder with your Atlas connection string 11 private const string MongoConnectionString = "<connection-string>"; 12 public void CreateVectorIndex() 13 { 14 try 15 { 16 // Connect to your Atlas cluster 17 var client = new MongoClient(MongoConnectionString); 18 var database = client.GetDatabase("sample_mflix"); 19 var collection = database.GetCollection<BsonDocument>("embedded_movies"); 20 21 var searchIndexView = collection.SearchIndexes; 22 var name = "vector_index"; 23 var type = SearchIndexType.VectorSearch; 24 25 var definition = new BsonDocument 26 { 27 { "fields", new BsonArray 28 { 29 new BsonDocument 30 { 31 { "type", "vector" }, 32 { "path", "plot_embedding" }, 33 { "numDimensions", 1536 }, 34 { "similarity", "dotProduct" }, 35 { "quantization", "scalar"} 36 }, 37 new BsonDocument 38 { 39 {"type", "filter"}, 40 {"path", "genres"} 41 }, 42 new BsonDocument 43 { 44 {"type", "filter"}, 45 {"path", "year"} 46 } 47 } 48 } 49 }; 50 51 var model = new CreateSearchIndexModel(name, type, definition); 52 53 searchIndexView.CreateOne(model); 54 Console.WriteLine($"New search index named {name} is building."); 55 56 // Polling for index status 57 Console.WriteLine("Polling to check if the index is ready. This may take up to a minute."); 58 bool queryable = false; 59 while (!queryable) 60 { 61 var indexes = searchIndexView.List(); 62 foreach (var index in indexes.ToEnumerable()) 63 { 64 if (index["name"] == name) 65 { 66 queryable = index["queryable"].AsBoolean; 67 } 68 } 69 if (!queryable) 70 { 71 Thread.Sleep(5000); 72 } 73 } 74 Console.WriteLine($"{name} is ready for querying."); 75 } 76 catch (Exception e) 77 { 78 Console.WriteLine($"Exception: {e.Message}"); 79 } 80 } 81 }
To create an Atlas Vector Search index for a collection using the MongoDB Go driver v1.16.0 or later, perform the following steps:
Create a .go
file and define the index in the file.
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 "go.mongodb.org/mongo-driver/mongo/options" 11 ) 12 13 func main() { 14 ctx := context.Background() 15 16 // Replace the placeholder with your Atlas connection string 17 const uri = "<connectionString>" 18 19 // Connect to your Atlas cluster 20 clientOptions := options.Client().ApplyURI(uri) 21 client, err := mongo.Connect(ctx, clientOptions) 22 if err != nil { 23 log.Fatalf("failed to connect to the server: %v", err) 24 } 25 defer func() { _ = client.Disconnect(ctx) }() 26 27 // Set the namespace 28 coll := client.Database("<databaseName>").Collection("<collectionName>") 29 30 // Define the index details 31 type vectorDefinitionField struct { 32 Type string `bson:"type"` 33 Path string `bson:"path"` 34 NumDimensions int `bson:"numDimensions"` 35 Similarity string `bson:"similarity"` 36 } 37 38 type vectorDefinition struct { 39 Fields []vectorDefinitionField `bson:"fields"` 40 } 41 42 indexName := "<indexName>" 43 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 44 45 indexModel := mongo.SearchIndexModel{ 46 Definition: vectorDefinition{ 47 Fields: []vectorDefinitionField{{ 48 Type: "vector", 49 Path: "<fieldToIndex>", 50 NumDimensions: <numberOfDimensions>, 51 Similarity: "euclidean | cosine | dotProduct"}}, 52 }, 53 Options: opts, 54 } 55 56 // Create the index 57 log.Println("Creating the index.") 58 searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, indexModel) 59 if err != nil { 60 log.Fatalf("failed to create the search index: %v", err) 61 } 62 63 // Await the creation of the index. 64 log.Println("Polling to confirm successful index creation.") 65 log.Println("NOTE: This may take up to a minute.") 66 searchIndexes := coll.SearchIndexes() 67 var doc bson.Raw 68 for doc == nil { 69 cursor, err := searchIndexes.List(ctx, options.SearchIndexes().SetName(searchIndexName)) 70 if err != nil { 71 fmt.Errorf("failed to list search indexes: %w", err) 72 } 73 74 if !cursor.Next(ctx) { 75 break 76 } 77 78 name := cursor.Current.Lookup("name").StringValue() 79 queryable := cursor.Current.Lookup("queryable").Boolean() 80 if name == searchIndexName && queryable { 81 doc = cursor.Current 82 } else { 83 time.Sleep(5 * time.Second) 84 } 85 } 86 87 log.Println("Name of Index Created: " + searchIndexName) 88 }
Example
Create a file named vector-index.go
.
Replace the following values and save the file.
| Atlas connection string. To learn more, see Connect via Drivers. |
| Database that contains the collection for which you want to create the index. |
| Collection for which you want to create the index. |
| Name of your index. If you omit the index name, Atlas Search names the index |
| Number of vector dimensions that Atlas Vector Search enforces at index-time and query-time. |
| Vector and filter fields to index. |
Example
Copy and paste the following into the vector-index.go
file
and replace the <connectionString>
placeholder value. The
following index definition indexes the plot_embedding
field
as the vector
type and the genres
and year
fields
as the filter
type in an Atlas Vector Search index. The
plot_embedding
field contains embeddings created using
OpenAI's text-embedding-ada-002
embeddings model. The index
definition specifies 1536
vector dimensions and measures
similarity using dotProduct
function.
The following index definition indexes only the vector
embeddings field (plot_embedding
) for performing vector search.
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "time" 8 9 "go.mongodb.org/mongo-driver/bson" 10 "go.mongodb.org/mongo-driver/mongo" 11 "go.mongodb.org/mongo-driver/mongo/options" 12 ) 13 14 func main() { 15 ctx := context.Background() 16 17 // Replace the placeholder with your Atlas connection string 18 const uri = "<connectionString>" 19 20 // Connect to your Atlas cluster 21 clientOptions := options.Client().ApplyURI(uri) 22 client, err := mongo.Connect(ctx, clientOptions) 23 if err != nil { 24 log.Fatalf("failed to connect to the server: %v", err) 25 } 26 defer func() { _ = client.Disconnect(ctx) }() 27 28 // Set the namespace 29 coll := client.Database("sample_mflix").Collection("embedded_movies") 30 31 // Define the index details 32 type vectorDefinitionField struct { 33 Type string `bson:"type"` 34 Path string `bson:"path"` 35 NumDimensions int `bson:"numDimensions"` 36 Similarity string `bson:"similarity"` 37 Quantization string `bson:"quantization"` 38 } 39 40 type vectorDefinition struct { 41 Fields []vectorDefinitionField `bson:"fields"` 42 } 43 44 indexName := "vector_index" 45 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 46 47 indexModel := mongo.SearchIndexModel{ 48 Definition: vectorDefinition{ 49 Fields: []vectorDefinitionField{{ 50 Type: "vector", 51 Path: "plot_embedding", 52 NumDimensions: 1536, 53 Similarity: "dotProduct", 54 Quantization: "scalar"}}, 55 }, 56 Options: opts, 57 } 58 59 // Create the index 60 searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, indexModel) 61 if err != nil { 62 log.Fatalf("failed to create the search index: %v", err) 63 } 64 log.Println("New search index named " + searchIndexName + " is building.") 65 66 // Await the creation of the index. 67 log.Println("Polling to check if the index is ready. This may take up to a minute.") 68 searchIndexes := coll.SearchIndexes() 69 var doc bson.Raw 70 for doc == nil { 71 cursor, err := searchIndexes.List(ctx, options.SearchIndexes().SetName(searchIndexName)) 72 if err != nil { 73 fmt.Errorf("failed to list search indexes: %w", err) 74 } 75 76 if !cursor.Next(ctx) { 77 break 78 } 79 80 name := cursor.Current.Lookup("name").StringValue() 81 queryable := cursor.Current.Lookup("queryable").Boolean() 82 if name == searchIndexName && queryable { 83 doc = cursor.Current 84 } else { 85 time.Sleep(5 * time.Second) 86 } 87 } 88 89 log.Println(searchIndexName + " is ready for querying.") 90 }
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "time" 8 9 "go.mongodb.org/mongo-driver/bson" 10 "go.mongodb.org/mongo-driver/mongo" 11 "go.mongodb.org/mongo-driver/mongo/options" 12 ) 13 14 func main() { 15 ctx := context.Background() 16 17 // Replace the placeholder with your Atlas connection string 18 const uri = "<connectionString>" 19 20 // Connect to your Atlas cluster 21 clientOptions := options.Client().ApplyURI(uri) 22 client, err := mongo.Connect(ctx, clientOptions) 23 if err != nil { 24 log.Fatalf("failed to connect to the server: %v", err) 25 } 26 defer func() { _ = client.Disconnect(ctx) }() 27 28 // Set the namespace 29 coll := client.Database("sample_mflix").Collection("embedded_movies") 30 indexName := "vector_index" 31 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 32 33 type vectorDefinitionField struct { 34 Type string `bson:"type"` 35 Path string `bson:"path"` 36 NumDimensions int `bson:"numDimensions"` 37 Similarity string `bson:"similarity"` 38 Quantization string `bson:"quantization"` 39 } 40 41 type filterField struct { 42 Type string `bson:"type"` 43 Path string `bson:"path"` 44 } 45 46 type indexDefinition struct { 47 Fields []vectorDefinitionField `bson:"fields"` 48 } 49 50 vectorDefinition := vectorDefinitionField{ 51 Type: "vector", 52 Path: "plot_embedding", 53 NumDimensions: 1536, 54 Similarity: "dotProduct", 55 Quantization: "scalar"} 56 genreFilterDefinition := filterField{"filter", "genres"} 57 yearFilterDefinition := filterField{"filter", "year"} 58 59 indexModel := mongo.SearchIndexModel{ 60 Definition: bson.D{{"fields", [3]interface{}{ 61 vectorDefinition, 62 genreFilterDefinition, 63 yearFilterDefinition}}}, 64 Options: opts, 65 } 66 67 // Create the index 68 searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, indexModel) 69 if err != nil { 70 log.Fatalf("failed to create the search index: %v", err) 71 } 72 log.Println("New search index named " + searchIndexName + " is building.") 73 74 // Await the creation of the index. 75 log.Println("Polling to check if the index is ready. This may take up to a minute.") 76 searchIndexes := coll.SearchIndexes() 77 var doc bson.Raw 78 for doc == nil { 79 cursor, err := searchIndexes.List(ctx, options.SearchIndexes().SetName(searchIndexName)) 80 if err != nil { 81 fmt.Errorf("failed to list search indexes: %w", err) 82 } 83 84 if !cursor.Next(ctx) { 85 break 86 } 87 88 name := cursor.Current.Lookup("name").StringValue() 89 queryable := cursor.Current.Lookup("queryable").Boolean() 90 if name == searchIndexName && queryable { 91 doc = cursor.Current 92 } else { 93 time.Sleep(5 * time.Second) 94 } 95 } 96 97 log.Println(searchIndexName + " is ready for querying.") 98 }
To create an Atlas Vector Search index for a collection using the MongoDB Java driver v5.2.0 or later, perform the following steps:
Create a .java
file and define the index in the file.
1 import com.mongodb.client.ListSearchIndexesIterable; 2 import com.mongodb.client.MongoClient; 3 import com.mongodb.client.MongoClients; 4 import com.mongodb.client.MongoCollection; 5 import com.mongodb.client.MongoCursor; 6 import com.mongodb.client.MongoDatabase; 7 import com.mongodb.client.model.SearchIndexModel; 8 import com.mongodb.client.model.SearchIndexType; 9 import org.bson.Document; 10 import org.bson.conversions.Bson; 11 12 import java.util.Arrays; 13 import java.util.Collections; 14 import java.util.List; 15 16 public class VectorIndex { 17 18 public static void main(String[] args) { 19 20 // Replace the placeholder with your Atlas connection string 21 String uri = "<connectionString>"; 22 23 // Connect to your Atlas cluster 24 try (MongoClient mongoClient = MongoClients.create(uri)) { 25 26 // Set the namespace 27 MongoDatabase database = mongoClient.getDatabase("<databaseName>"); 28 MongoCollection<Document> collection = database.getCollection("<collectionName>"); 29 30 // Define the index details 31 String indexName = "<indexName>"; 32 Bson definition = new Document( 33 "fields", 34 Arrays.asList( 35 new Document("type", "vector") 36 .append("path", "<fieldToIndex>") 37 .append("numDimensions", <numberOfDimensions>) 38 .append("similarity", "euclidean | cosine | dotProduct"), 39 new Document("type", "filter") 40 .append("path", "<fieldToIndex>"), 41 ... 42 ) 43 ); 44 45 // Define the index model 46 SearchIndexModel indexModel = new SearchIndexModel( 47 indexName, 48 definition, 49 SearchIndexType.vectorSearch() 50 ); 51 52 // Create the index 53 try { 54 List<String> result = collection.createSearchIndexes(Collections.singletonList(indexModel)); 55 System.out.println("New search index named " + result.get(0) + " is building."); 56 } catch (Exception e) { 57 throw new RuntimeException("Error creating index: " + e); 58 } 59 60 61 // Wait for Atlas to build the index 62 System.out.println("Polling to check if the index is ready. This may take up to a minute."); 63 64 ListSearchIndexesIterable<Document> searchIndexes = collection.listSearchIndexes(); 65 Document doc = null; 66 while (doc == null) { 67 try (MongoCursor<Document> cursor = searchIndexes.iterator()) { 68 if (!cursor.hasNext()) { 69 break; 70 } 71 Document current = cursor.next(); 72 String name = current.getString("name"); 73 // When the index completes building, it becomes `queryable` 74 boolean queryable = current.getBoolean("queryable"); 75 if (name.equals(indexName) && queryable) { 76 doc = current; 77 } else { 78 Thread.sleep(500); 79 } 80 } catch (Exception e) { 81 throw new RuntimeException("Failed to list search indexes: " + e); 82 mongoClient.close(); 83 } 84 } 85 System.out.println(indexName + " is ready for querying."); 86 87 } catch (Exception e) { 88 throw new RuntimeException("Error connecting to MongoDB: " + e); 89 } 90 } 91 }
Replace the following values and save the file.
| Atlas connection string. To learn more, see Connect via Drivers. |
| Database that contains the collection for which you want to create the index. |
| Collection for which you want to create the index. |
| Name of your index. If you omit the index name, Atlas Search names the index |
| Number of vector dimensions that Atlas Vector Search enforces at index-time and query-time. |
| Vector and filter fields to index. |
The following example index definitions:
Index the
plot_embedding
field as thevector
type and thegenres
andyear
fields as thefilter
type in an Atlas Vector Search index.Specifies the
plot_embedding
field as the vector embeddings field, which contains embeddings created using OpenAI'stext-embedding-ada-002
embeddings model.Specifies
1536
vector dimensions and measures similarity usingdotProduct
function.
This index definition indexes only the vector embeddings field (plot_embedding
) for performing vector search.
Copy and paste the following into the file you created, and replace the <connectionString>
placeholder value.
1 import com.mongodb.client.ListSearchIndexesIterable; 2 import com.mongodb.client.MongoClient; 3 import com.mongodb.client.MongoClients; 4 import com.mongodb.client.MongoCollection; 5 import com.mongodb.client.MongoCursor; 6 import com.mongodb.client.MongoDatabase; 7 import com.mongodb.client.model.SearchIndexModel; 8 import com.mongodb.client.model.SearchIndexType; 9 import org.bson.Document; 10 import org.bson.conversions.Bson; 11 12 import java.util.Collections; 13 import java.util.List; 14 15 public class VectorIndex { 16 17 public static void main(String[] args) { 18 19 // Replace the placeholder with your Atlas connection string 20 String uri = "<connectionString>"; 21 22 // Connect to your Atlas cluster 23 try (MongoClient mongoClient = MongoClients.create(uri)) { 24 25 // Set the namespace 26 MongoDatabase database = mongoClient.getDatabase("sample_mflix"); 27 MongoCollection<Document> collection = database.getCollection("embedded_movies"); 28 29 // Define the index details 30 String indexName = "vector_index"; 31 Bson definition = new Document( 32 "fields", 33 Collections.singletonList( 34 new Document("type", "vector") 35 .append("path", "plot_embedding") 36 .append("numDimensions", 1536) 37 .append("similarity", "dotProduct") 38 .append("quantization", "scalar"))); 39 40 // Define the index model 41 SearchIndexModel indexModel = new SearchIndexModel( 42 indexName, 43 definition, 44 SearchIndexType.vectorSearch()); 45 46 // Create the index 47 try { 48 List<String> result = collection.createSearchIndexes(Collections.singletonList(indexModel)); 49 System.out.println("New search index named " + result.get(0) + " is building."); 50 } catch (Exception e) { 51 throw new RuntimeException("Error creating index: " + e); 52 } 53 54 // Wait for Atlas to build the index 55 System.out.println("Polling to check if the index is ready. This may take up to a minute."); 56 57 ListSearchIndexesIterable<Document> searchIndexes = collection.listSearchIndexes(); 58 Document doc = null; 59 while (doc == null) { 60 try (MongoCursor<Document> cursor = searchIndexes.iterator()) { 61 if (!cursor.hasNext()) { 62 break; 63 } 64 Document current = cursor.next(); 65 String name = current.getString("name"); 66 // When the index completes building, it becomes `queryable` 67 boolean queryable = current.getBoolean("queryable"); 68 if (name.equals(indexName) && queryable) { 69 doc = current; 70 } else { 71 Thread.sleep(500); 72 } 73 } catch (Exception e) { 74 throw new RuntimeException("Failed to list search indexes: " + e); 75 } 76 } 77 System.out.println(indexName + " is ready for querying."); 78 79 } catch (Exception e) { 80 throw new RuntimeException("Error connecting to MongoDB: " + e); 81 } 82 } 83 }
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
Copy and paste the following into the file you created, and replace
the <connectionString>
placeholder value.
1 import com.mongodb.client.ListSearchIndexesIterable; 2 import com.mongodb.client.MongoClient; 3 import com.mongodb.client.MongoClients; 4 import com.mongodb.client.MongoCollection; 5 import com.mongodb.client.MongoCursor; 6 import com.mongodb.client.MongoDatabase; 7 import com.mongodb.client.model.SearchIndexModel; 8 import com.mongodb.client.model.SearchIndexType; 9 import org.bson.Document; 10 import org.bson.conversions.Bson; 11 12 import java.util.Arrays; 13 import java.util.Collections; 14 import java.util.List; 15 16 public class VectorIndex { 17 18 public static void main(String[] args) { 19 20 // Replace the placeholder with your Atlas connection string 21 String uri = "<connectionString>"; 22 23 // Connect to your Atlas cluster 24 try (MongoClient mongoClient = MongoClients.create(uri)) { 25 26 // Set the namespace 27 MongoDatabase database = mongoClient.getDatabase("sample_mflix"); 28 MongoCollection<Document> collection = database.getCollection("embedded_movies"); 29 30 // Define the index details with the filter fields 31 String indexName = "vector_index"; 32 Bson definition = new Document( 33 "fields", 34 Arrays.asList( 35 new Document("type", "vector") 36 .append("path", "plot_embedding") 37 .append("numDimensions", 1536) 38 .append("similarity", "dotProduct") 39 .append("quantization", "scalar"), 40 new Document("type", "filter") 41 .append("path", "genres"), 42 new Document("type", "filter") 43 .append("path", "year"))); 44 45 // Define the index model 46 SearchIndexModel indexModel = new SearchIndexModel( 47 indexName, 48 definition, 49 SearchIndexType.vectorSearch()); 50 51 // Create the filtered index 52 try { 53 List<String> result = collection.createSearchIndexes(Collections.singletonList(indexModel)); 54 System.out.println("New search index named " + result.get(0) + " is building."); 55 } catch (Exception e) { 56 throw new RuntimeException("Error creating index: " + e); 57 } 58 59 // Wait for Atlas to build the index 60 System.out.println("Polling to check if the index is ready. This may take up to a minute."); 61 62 ListSearchIndexesIterable<Document> searchIndexes = collection.listSearchIndexes(); 63 Document doc = null; 64 while (doc == null) { 65 try (MongoCursor<Document> cursor = searchIndexes.iterator()) { 66 if (!cursor.hasNext()) { 67 break; 68 } 69 Document current = cursor.next(); 70 String name = current.getString("name"); 71 // When the index completes building, it becomes `queryable` 72 boolean queryable = current.getBoolean("queryable"); 73 if (name.equals(indexName) && queryable) { 74 doc = current; 75 } else { 76 Thread.sleep(500); 77 } 78 } catch (Exception e) { 79 throw new RuntimeException("Failed to list search indexes: " + e); 80 mongoClient.close(); 81 } 82 } 83 System.out.println(indexName + " is ready for querying."); 84 85 } catch (Exception e) { 86 throw new RuntimeException("Error connecting to MongoDB: " + e); 87 } 88 } 89 }
To create an Atlas Vector Search index for a collection using the MongoDB Node driver v6.6.0 or later, perform the following steps:
Create a .js
file and define the index in the file.
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("<databaseName>"); 11 const collection = database.collection("<collectionName>"); 12 13 // define your Atlas Vector Search index 14 const index = { 15 name: "<indexName>", 16 type: "vectorSearch", 17 definition: { 18 "fields": [ 19 { 20 "type": "vector", 21 "numDimensions": <numberOfDimensions>, 22 "path": "<fieldToIndex>", 23 "similarity": "euclidean | cosine | dotProduct" 24 }, 25 { 26 "type": "filter", 27 "path": "<fieldToIndex>" 28 }, 29 ... 30 ] 31 } 32 } 33 34 // run the helper method 35 const result = await collection.createSearchIndex(index); 36 console.log(`New search index named ${result} is building.`); 37 // wait for the index to be ready to query 38 console.log("Polling to check if the index is ready. This may take up to a minute.") 39 let isQueryable = false; 40 while (!isQueryable) { 41 const cursor = collection.listSearchIndexes(); 42 for await (const index of cursor) { 43 if (index.name === result) { 44 if (index.queryable) { 45 console.log(`${result} is ready for querying.`); 46 isQueryable = true; 47 } else { 48 await new Promise(resolve => setTimeout(resolve, 5000)); 49 } 50 } 51 } 52 } 53 } finally { 54 await client.close(); 55 } 56 } 57 run().catch(console.dir);
Example
Create a file named vector-index.js
.
Replace the following values and save the file.
| Atlas connection string. To learn more, see Connect via Drivers. |
| Database that contains the collection for which you want to create the index. |
| Collection for which you want to create the index. |
| Name of your index. If you omit the index name, Atlas Search names the index |
| Number of vector dimensions that Atlas Vector Search enforces at index-time and query-time. |
| Vector and filter fields to index. |
Example
Copy and paste the following into the vector-index.js
file
and replace the <connectionString>
placeholder value. The
following index definition indexes the plot_embedding
field
as the vector
type and the genres
and year
fields
as the filter
type in an Atlas Vector Search index. The
plot_embedding
field contains embeddings created using
OpenAI's text-embedding-ada-002
embeddings model. The index
definition specifies 1536
vector dimensions and measures
similarity using dotProduct
function.
The following index definition indexes only the vector
embeddings field (plot_embedding
) for performing vector search.
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("sample_mflix"); 11 const collection = database.collection("embedded_movies"); 12 13 // define your Atlas Vector Search index 14 const index = { 15 name: "vector_index", 16 type: "vectorSearch", 17 definition: { 18 "fields": [ 19 { 20 "type": "vector", 21 "numDimensions": 1536, 22 "path": "plot_embedding", 23 "similarity": "dotProduct", 24 "quantization": "scalar" 25 } 26 ] 27 } 28 } 29 30 // run the helper method 31 const result = await collection.createSearchIndex(index); 32 console.log(`New search index named ${result} is building.`); 33 34 // wait for the index to be ready to query 35 console.log("Polling to check if the index is ready. This may take up to a minute.") 36 let isQueryable = false; 37 while (!isQueryable) { 38 const cursor = collection.listSearchIndexes(); 39 for await (const index of cursor) { 40 if (index.name === result) { 41 if (index.queryable) { 42 console.log(`${result} is ready for querying.`); 43 isQueryable = true; 44 } else { 45 await new Promise(resolve => setTimeout(resolve, 5000)); 46 } 47 } 48 } 49 } 50 } finally { 51 await client.close(); 52 } 53 } 54 run().catch(console.dir);
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("sample_mflix"); 11 const collection = database.collection("embedded_movies"); 12 13 // define your Atlas Vector Search index 14 const index = { 15 name: "vector_index", 16 type: "vectorSearch", 17 definition: { 18 "fields": [ 19 { 20 "type": "vector", 21 "numDimensions": 1536, 22 "path": "plot_embedding", 23 "similarity": "dotProduct", 24 "quantization": "scalar" 25 }, 26 { 27 "type": "filter", 28 "path": "genres" 29 }, 30 { 31 "type": "filter", 32 "path": "year" 33 } 34 ] 35 } 36 } 37 38 // run the helper method 39 const result = await collection.createSearchIndex(index); 40 console.log(`New search index named ${result} is building.`); 41 42 // wait for the index to be ready to query 43 console.log("Polling to check if the index is ready. This may take up to a minute.") 44 let isQueryable = false; 45 while (!isQueryable) { 46 const cursor = collection.listSearchIndexes(); 47 for await (const index of cursor) { 48 if (index.name === result) { 49 if (index.queryable) { 50 console.log(`${result} is ready for querying.`); 51 isQueryable = true; 52 } else { 53 await new Promise(resolve => setTimeout(resolve, 5000)); 54 } 55 } 56 } 57 } 58 } finally { 59 await client.close(); 60 } 61 } 62 run().catch(console.dir);
To create an Atlas Vector Search index for a collection using the PyMongo driver v4.7 or later, perform the following steps:
Create a .py
file and define the index in the file.
1 import pymongo 2 from pymongo.mongo_client import MongoClient 3 from pymongo.operations import SearchIndexModel 4 5 # Connect to your Atlas deployment 6 uri = "<connectionString>" 7 client = MongoClient(uri) 8 9 # Access your database and collection 10 database = client["<databaseName>"] 11 collection = database["<collectionName>"] 12 13 # Create your index model, then create the search index 14 search_index_model = SearchIndexModel( 15 definition={ 16 "fields": [ 17 { 18 "type": "vector", 19 "numDimensions": <numberofDimensions>, 20 "path": "<fieldToIndex>", 21 "similarity": "euclidean | cosine | dotProduct" 22 }, 23 { 24 "type": "filter", 25 "path": "<fieldToIndex>" 26 }, 27 ... 28 ] 29 }, 30 name="<indexName>", 31 type="vectorSearch", 32 ) 33 34 result = collection.create_search_index(model=search_index_model) 35 print("New search index named " + result + " is building.") 36 37 # Wait for initial sync to complete 38 print("Polling to check if the index is ready. This may take up to a minute.") 39 predicate=None 40 if predicate is None: 41 predicate = lambda index: index.get("queryable") is True 42 43 while True: 44 indices = list(collection.list_search_indexes(result)) 45 if len(indices) and predicate(indices[0]): 46 break 47 time.sleep(5) 48 print(result + " is ready for querying.") 49 50 client.close()
To learn more, see the create_search_index() method.
1 from pymongo.mongo_client import MongoClient 2 from pymongo.operations import SearchIndexModel 3 4 def create_indexes(): 5 # Connect to your Atlas deployment 6 uri = "<connectionString>" 7 client = MongoClient(uri) 8 9 # Access your database and collection 10 database = client["<databaseName>"] 11 collection = database["<collectionName>"] 12 13 # Create your index models and add them to an array 14 first_model = SearchIndexModel( 15 definition={ 16 "fields": [ 17 { 18 "type": "vector", 19 "numDimensions": <numberOfDimensions>, 20 "path": "<fieldToIndex>", 21 "similarity": "euclidean | cosine | dotProduct" 22 }, 23 { 24 "type": "filter", 25 "path": "<fieldToIndex>" 26 }, 27 ... 28 ] 29 }, 30 name="<indexName>", 31 type="vectorSearch", 32 ) 33 34 second_model = SearchIndexModel( 35 definition={ 36 "fields": [ 37 { 38 "type": "vector", 39 "numDimensions": <numberOfDimensions>, 40 "path": "<fieldToIndex>", 41 "similarity": "euclidean | cosine | dotProduct" 42 }, 43 { 44 "type": "filter", 45 "path": "<fieldToIndex>" 46 }, 47 ... 48 ] 49 }, 50 name="<index name>", 51 type="vectorSearch", 52 ) 53 54 ... 55 56 idx_models = [first_model, second_model, ...] 57 58 # Create the search indexes 59 result = collection.create_search_indexes(models=idx_models) 60 print(result) 61 62 client.close()
To learn more, see the create_search_indexes() method.
Example
Create a file named vector-index.py
.
Replace the following values and save the file.
| Atlas connection string. To learn more, see Connect via Drivers. |
| Database that contains the collection for which you want to create the index. |
| Collection for which you want to create the index. |
| Name of your index. If you omit the index name, Atlas Search names the index |
| Number of vector dimensions that Atlas Vector Search enforces at index-time and query-time. |
| Vector and filter fields to index. |
Example
Copy and paste the following into the vector-index.py
and
replace the <connectionString>
placeholder value. The following index
definition indexes the plot_embedding
field as the
vector
type and the genres
and year
fields as the
filter
type in an Atlas Vector Search index. The plot_embedding
field contains embeddings created using OpenAI's
text-embedding-ada-002
embeddings model. The index
definition specifies 1536
vector dimensions and measures
similarity using dotProduct
function.
Tip
Work with a runnable version of this example as a Python notebook.
The following index definition indexes only the vector
embeddings field (plot_embedding
) for performing
vector search.
1 from pymongo.mongo_client import MongoClient 2 from pymongo.operations import SearchIndexModel 3 import time 4 5 # Connect to your Atlas deployment 6 uri = "<connectionString>" 7 client = MongoClient(uri) 8 9 # Access your database and collection 10 database = client["sample_mflix"] 11 collection = database["embedded_movies"] 12 13 # Create your index model, then create the search index 14 search_index_model = SearchIndexModel( 15 definition={ 16 "fields": [ 17 { 18 "type": "vector", 19 "path": "plot_embedding", 20 "numDimensions": 1536, 21 "similarity": "dotProduct", 22 "quantization": "scalar" 23 } 24 ] 25 }, 26 name="vector_index", 27 type="vectorSearch", 28 ) 29 30 result = collection.create_search_index(model=search_index_model) 31 print("New search index named " + result + " is building.") 32 33 # Wait for initial sync to complete 34 print("Polling to check if the index is ready. This may take up to a minute.") 35 predicate=None 36 if predicate is None: 37 predicate = lambda index: index.get("queryable") is True 38 39 while True: 40 indices = list(collection.list_search_indexes(result)) 41 if len(indices) and predicate(indices[0]): 42 break 43 time.sleep(5) 44 print(result + " is ready for querying.") 45 46 client.close()
Tip
Work with a runnable version of this example as a Python notebook.
This index definition indexes the following fields:
A string field (
genres
) and a numeric field (year
) for pre-filtering the data.The vector embeddings field (
plot_embedding
) for performing vector search against pre-filtered data.
1 from pymongo.mongo_client import MongoClient 2 from pymongo.operations import SearchIndexModel 3 import time 4 5 # Connect to your Atlas deployment 6 uri = "<connectionString>" 7 client = MongoClient(uri) 8 9 # Access your database and collection 10 database = client["sample_mflix"] 11 collection = database["embedded_movies"] 12 13 # Create your index model, then create the search index 14 search_index_model = SearchIndexModel( 15 definition={ 16 "fields": [ 17 { 18 "type": "vector", 19 "path": "plot_embedding", 20 "numDimensions": 1536, 21 "similarity": "dotProduct", 22 "quantization": "scalar" 23 }, 24 { 25 "type": "filter", 26 "path": "genres" 27 }, 28 { 29 "type": "filter", 30 "path": "year" 31 } 32 ] 33 }, 34 name="vector_index", 35 type="vectorSearch", 36 ) 37 38 result = collection.create_search_index(model=search_index_model) 39 print("New search index named " + result + " is building.") 40 41 # Wait for initial sync to complete 42 print("Polling to check if the index is ready. This may take up to a minute.") 43 predicate=None 44 if predicate is None: 45 predicate = lambda index: index.get("queryable") is True 46 47 while True: 48 indices = list(collection.list_search_indexes(result)) 49 if len(indices) and predicate(indices[0]): 50 break 51 time.sleep(5) 52 print(result + " is ready for querying.") 53 54 client.close()
View an Atlas Vector Search Index
You can view Atlas Vector Search indexes for all collections from the
Atlas UI, Atlas Administration API, Atlas CLI, mongosh
,
or a supported MongoDB Driver.
Required Access
You need the Project Search Index Editor
or higher role to view
Atlas Vector Search indexes.
Note
You can use the mongosh
command or driver helper methods to retrieve
Atlas Search indexes on all Atlas cluster tiers. For a list of supported driver
versions, see Supported Clients.
Procedure
➤ Use the Select your language drop-down menu to set the language of the example in this section.
To retrieve all the Atlas Vector Search indexes for a collection using the
Atlas Administration API, send a GET
request to the Atlas Search
indexes
endpoint with the name of the database and collection.
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --include \ 4 --request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}"
To learn more about the syntax and parameters for the endpoint, see Return All Atlas Search Indexes for One Collection.
To retrieve one Atlas Vector Search index for a collection using the
Atlas Administration API, send a GET
request to the Atlas Search
indexes
endpoint with either the unique ID or name of the
index (line 4) to retrieve.
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --include \ 4 --request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{indexId} | https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}/{indexName|indexId}"
To learn more about the syntax and parameters for the endpoint, Get One By Name and Get One By ID.
To return Atlas Vector Search indexes for a collection using Atlas CLI, perform the following steps:
Run the following command to retrieve the indexes for the collection.
atlas clusters search indexes list --clusterName [cluster_name] --db <db-name> --collection <collection-name>
In the command, replace the following placeholder values:
cluster-name
- the name of the Atlas cluster that contains the indexed collection.db-name
- the name of the database that contains the collection for which you want to retrieve the indexes.collection-name
- the name of the collection for which you want to retrieve the indexes.
To learn more about the command syntax and parameters, see the Atlas CLI documentation for the atlas clusters search indexes list command.
In Atlas, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
The page displays the following details for the indexes on the page:
Name | Label that identifies the index. |
Index Type | Label that indicates an Atlas Search or Atlas Vector Search index. Values include:
|
Index Fields | List that contains the fields that this index indexes. |
Status | Current state of the index on the primary node of the cluster. For valid values, see Index Status. |
Size | Size of the index on the primary node. |
Documents | Number of indexed documents out of the total number of documents in the collection. |
Actions | Actions that you can take on the index. You can: You can't run queries in the Search Tester UI
against indexes of the |
To view an Atlas Vector Search index for a collection using mongosh
,
perform the following steps:
Connect to the Atlas cluster using mongosh
.
To learn more, see Connect via mongosh
.
Run the db.collection.getSearchIndexes()
method.
The db.collection.getSearchIndexes()
method has the
following syntax:
1 db.<collectionName>.getSearchIndexes( "<index-name>" );
To view Atlas Vector Search indexes for a collection using C# driver 3.1.0 or later, perform the following steps:
Create a .cs
file and use the .List()
method to retrieve the indexes for the collection.
1 namespace query_quick_start; 2 3 using MongoDB.Bson; 4 using MongoDB.Driver; 5 6 public class IndexService 7 { 8 private const string MongoConnectionString = "<connectionString>"; 9 // Other class methods here... 10 public void ViewSearchIndexes() 11 { 12 try 13 { 14 // Connect to your Atlas deployment 15 var client = new MongoClient(MongoConnectionString); 16 17 // Access your database and collection 18 var database = client.GetDatabase("<databaseName>"); 19 var collection = database.GetCollection<BsonDocument>("<collectionName>"); 20 21 // Get a list of the collection's search indexes and print them 22 var searchIndexView = collection.SearchIndexes; 23 var indexes = searchIndexView.List(); 24 25 foreach (var index in indexes.ToEnumerable()) 26 { 27 Console.WriteLine(index); 28 } 29 } 30 catch (Exception e) 31 { 32 Console.WriteLine($"Exception: {e.Message}"); 33 } 34 } 35 }
Replace the following values and save the file.
| Your Atlas connection string. To learn more, see Connect via Drivers. |
| The name of the database that contains the collection. |
| The name of the collection. |
To view an Atlas Vector Search index for a collection using MongoDB Go driver v1.16.0 or later, perform the following steps:
Create the .go
file and use the SearchIndexes().List()
method to retrieve the index.
1 package main 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "log" 8 9 "go.mongodb.org/mongo-driver/bson" 10 "go.mongodb.org/mongo-driver/mongo" 11 "go.mongodb.org/mongo-driver/mongo/options" 12 ) 13 14 func main() { 15 ctx := context.Background() 16 17 // Replace the placeholder with your Atlas connection string 18 const uri = "<connectionString>" 19 20 // Connect to your Atlas cluster 21 clientOptions := options.Client().ApplyURI(uri) 22 client, err := mongo.Connect(ctx, clientOptions) 23 if err != nil { 24 log.Fatalf("failed to connect to the server: %v", err) 25 } 26 defer func() { _ = client.Disconnect(ctx) }() 27 28 // Set the namespace 29 coll := client.Database("<databaseName>").Collection("<collectionName>") 30 31 // Specify the options for the index to retrieve 32 indexName := "<indexName>" 33 opts := options.SearchIndexes().SetName(indexName) 34 35 // Get the index 36 cursor, err := coll.SearchIndexes().List(ctx, opts) 37 if err != nil { 38 log.Fatalf("failed to get the index: %v", err) 39 } 40 41 // Print the index details to the console as JSON 42 var results []bson.M 43 if err := cursor.All(ctx, &results); err != nil { 44 log.Fatalf("failed to unmarshal results to bson: %v", err) 45 } 46 res, err := json.Marshal(results) 47 if err != nil { 48 log.Fatalf("failed to marshal results to json: %v", err) 49 } 50 fmt.Println(string(res)) 51 }
Replace the following values and save the file.
| Your Atlas connection string. To learn more, see Connect via Drivers. |
| The database that contains the collection. |
| The collection for which you want to retrieve the indexes. |
| The name of your index if you want to retrieve a specific index. To return all indexes on the collection, omit this value. |
To view an Atlas Vector Search index for a collection using the MongoDB Java driver v5.2.0 or later, perform the following steps:
Create a .java
file and use the listSearchIndexes()
method to retrieve the index.
1 import com.mongodb.client.MongoClient; 2 import com.mongodb.client.MongoClients; 3 import com.mongodb.client.MongoCollection; 4 import com.mongodb.client.MongoDatabase; 5 import org.bson.Document; 6 7 public class ViewVectorIndex { 8 9 public static void main(String[] args) { 10 11 // Replace the placeholder with your Atlas connection string 12 String uri = "<connectionString>"; 13 14 // Connect to your Atlas cluster 15 try (MongoClient mongoClient = MongoClients.create(uri)) { 16 17 // Set the namespace 18 MongoDatabase database = mongoClient.getDatabase("<databaseName>"); 19 MongoCollection<Document> collection = database.getCollection("<collectionName>"); 20 21 // Specify the options for the index to retrieve 22 String indexName = "<indexName>"; 23 24 // Get the index and print details to the console as JSON 25 try { 26 Document listSearchIndex = collection.listSearchIndexes().name(indexName).first(); 27 if (listSearchIndex != null) { 28 System.out.println("Index found: " + listSearchIndex.toJson()); 29 } else { 30 System.out.println("Index not found."); 31 } 32 } catch (Exception e) { 33 throw new RuntimeException("Error finding index: " + e); 34 } 35 36 } catch (Exception e) { 37 throw new RuntimeException("Error connecting to MongoDB: " + e); 38 } 39 } 40 }
Replace the following values and save the file.
| Your Atlas connection string. To learn more, see Connect via Drivers. |
| The database that contains the collection. |
| The collection for which you want to retrieve the indexes. |
| The name of your index if you want to retrieve a specific index. To return all indexes on the collection, omit this value. |
To view an Atlas Vector Search index for a collection using MongoDB Node driver v6.6.0 or later, perform the following steps:
Create the .js
file and use the listSearchIndexes()
method to retrieve the index.
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("<databaseName>"); 11 const collection = database.collection("<collectionName>"); 12 13 // run the helper method 14 const result = await collection.listSearchIndexes("<indexName>").toArray(); 15 console.log(result); 16 } finally { 17 await client.close(); 18 } 19 } 20