Atlas Vector Search Quick Start
On this page
This quick start describes how to load sample documents that contain vector embeddings into an Atlas cluster or local Atlas deployment, create an Atlas Vector Search index on those embeddings, and then perform semantic search to return documents that are similar to your query.
Time required: 15 minutes
Objectives
In this quick start, you will do the following steps:
Create an index definition for the
sample_mflix.embedded_movies
collection that indexes theplot_embedding
field as thevector
type. Theplot_embedding
field contains embeddings created using OpenAI'stext-embedding-ada-002
embedding model. The index definition specifies1536
vector dimensions and measures similarity usingdotProduct
.Run an Atlas Vector Search query that searches the sample
sample_mflix.embedded_movies
collection. The query uses the$vectorSearch
stage to search theplot_embedding
field, which contains embeddings created using OpenAI'stext-embedding-ada-002
embedding model. The query searches theplot_embedding
field using vector embeddings for the string time travel. It considers up to150
nearest neighbors, and returns10
documents in the results.
To learn more, see Learning Summary.
Create a Vector Search Index
➤ To set the client you use to run the examples on this page, use the Select your language drop-down menu in the right navigation pane.
In this section, you create an Atlas Vector Search index on sample data that you load into an Atlas cluster or a deployment hosted on your local computer:
Set up your Atlas cluster.
Create a free Atlas account or sign in to an existing account.
If you don't yet have an Atlas cluster, create a free M0 cluster. To learn more about creating an Atlas cluster, see Create a Cluster.
Note
If you are working with an existing cluster, you must have
Project Data Access Admin
or higher access to your Atlas project.If you create a new cluster, you have the necessary permissions by default.
You can create only one
M0
Free cluster per project.In the left sidebar, click Atlas Search. Choose your cluster from the Select data source menu and click Go to Atlas Search.
If you haven't yet loaded the sample dataset onto your cluster, click Load a Sample Dataset. In the Load Sample Dataset dialog box, click Load Sample Dataset to confirm.
If you already loaded the sample dataset, check that the
sample_mflix
database contains theembedded_movies
collection. If it doesn't, drop the sample databases and reload the sample dataset.Loading the sample dataset can take several minutes to complete.
Create a 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.
When the sample data finishes loading, click Create Search Index.
Under the Atlas Vector Search section, select the JSON Editor and click Next.
In the Database and Collection section, expand the
sample_mflix
database and select theembedded_movies
collection.Each document in this collection contains information about a movie, including a summary of the movie's plot as a string, which has also been converted to and stored as a vector embedding in the document's
plot_embedding
field.In the Index Name field, specify
vector_index
.Copy and paste the following vector search index definition into the JSON Editor.
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Click Next.
Click Create Search Index.
The index should take about one minute to build. When your vector index is finished building, the Status column reads Active.
Connect to the Atlas cluster using
mongosh
.Open
mongosh
in a terminal window and connect to your Atlas cluster. For detailed instructions on connecting, see Connect via mongosh.Switch to the database that contains the collection for which you want to create the index.
Example
use sample_mflix switched to db sample_mflix Run the
db.collection.createSearchIndex()
method.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": "euclidean" 11 } 12 ] 13 } 14 ); This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Install the MongoDB C Driver.
For detailed installation instructions, refer to the MongoDB C Driver documentation.
Create a new directory called
query-quick-start
.mkdir query-quick-start Enter the directory, and create a
CMakeLists.txt
file.cd query-quick-start touch CMakeLists.txt Copy and paste the following lines into the
CMakeLists.txt
file:cmake_minimum_required(VERSION 3.30) project(atlas-vector-search-quick-start) # Specify the minimum version for creating a vector index. find_package (mongoc-1.0 1.28.0 REQUIRED) add_executable(atlas-vector-search-quick-start vector_index.c ) target_link_libraries (atlas-vector-search-quick-start PRIVATE mongo::mongoc_shared) Define the index.
Create a file named
vector_index.c
. Copy and paste the following code into the file.vector_index.c1 2 3 4 5 int main(void) { 6 mongoc_client_t *client; 7 mongoc_collection_t *collection; 8 bson_error_t error; 9 char database_name[] = "sample_mflix"; 10 char collection_name[] = "embedded_movies"; 11 char index_name[] = "vector_index"; 12 13 mongoc_init(); 14 15 // Replace the placeholder with your Atlas connection string 16 client = mongoc_client_new("<connection-string>"); 17 18 // Connect to your Atlas cluster 19 collection = mongoc_client_get_collection (client, database_name, collection_name); 20 21 bson_t cmd; 22 // Create search index command. 23 { 24 char *cmd_str = bson_strdup_printf ( 25 BSON_STR ({ 26 "createSearchIndexes" : "%s", 27 "indexes" : [{ 28 "definition": { 29 "fields": [{ 30 "type": "vector", 31 "path": "plot_embedding", 32 "numDimensions": 1536, 33 "similarity": "dotProduct" 34 }] 35 }, 36 "name": "%s", 37 "type": "vectorSearch" 38 }] 39 }), 40 collection_name, index_name); 41 if (!bson_init_from_json(&cmd, cmd_str, -1, &error)) { 42 printf("Failed to initialize BSON: %s\n", error.message); 43 bson_free(cmd_str); 44 return 1; 45 } 46 bson_free (cmd_str); 47 } 48 if (!mongoc_collection_command_simple (collection, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { 49 bson_destroy (&cmd); 50 printf ("Failed to run createSearchIndexes: %s", error.message); 51 return 1; 52 } else { 53 printf ("New search index named %s is building.\n", index_name); 54 bson_destroy (&cmd); 55 } 56 57 // Polling for index status 58 printf("Polling to check if the index is ready. This may take up to a minute.\n"); 59 int queryable = 0; 60 while (!queryable) { 61 const char *pipeline_str = "{\"pipeline\": [{\"$listSearchIndexes\": {}}]}"; 62 bson_t pipeline; 63 if (!bson_init_from_json(&pipeline, pipeline_str, -1, &error)) { 64 printf("Failed to initialize pipeline BSON: %s\n", error.message); 65 break; // Exit the loop on error 66 } 67 mongoc_cursor_t *cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL); 68 const bson_t *got; 69 // Check if the cursor returns any documents 70 int found_index = 0; 71 while (mongoc_cursor_next(cursor, &got)) { 72 bson_iter_t iter; 73 if (bson_iter_init(&iter, got) && bson_iter_find(&iter, "name")) { 74 const char *name = bson_iter_utf8(&iter, NULL); 75 if (strcmp(name, index_name) == 0) { 76 found_index = 1; // Index found 77 bson_iter_find(&iter, "queryable"); 78 queryable = bson_iter_bool(&iter); 79 break; // Exit the loop since we found the index 80 } 81 } 82 } 83 if (mongoc_cursor_error(cursor, &error)) { 84 printf("Failed to run $listSearchIndexes: %s\n", error.message); 85 break; // Exit the loop on error 86 } 87 if (!found_index) { 88 printf("Index %s not found yet. Retrying...\n", index_name); 89 } 90 bson_destroy(&pipeline); 91 mongoc_cursor_destroy(cursor); 92 sleep(5); // Sleep for 5 seconds before checking again 93 } 94 if (queryable) { 95 printf("%s is ready for querying.\n", index_name); 96 } else { 97 printf("Error occurred or index not found.\n"); 98 } 99 100 // Cleanup 101 mongoc_collection_destroy(collection); 102 mongoc_client_destroy(client); 103 mongoc_cleanup(); 104 105 return 0; 106 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Create and enter the
/build
directory:mkdir build && cd build Prepare the project.
cmake ../ Build the app.
cmake --build . Execute the app to create the index.
./atlas-vector-search-quick-start 1 New search index named vector_index is building. 2 Polling to check if the index is ready. This may take up to a minute. 3 vector_index is ready for querying.
Install the MongoDB C++ Driver.
For detailed installation instructions, refer to the MongoDB C++ Driver documentation.
Create a new directory called
query-quick-start
.mkdir query-quick-start Enter the directory, and create a
CMakeLists.txt
file.cd query-quick-start touch CMakeLists.txt Copy and paste the following lines into the
CMakeLists.txt
file:cmake_minimum_required(VERSION 3.30) project(query_quick_start) set(CMAKE_CXX_STANDARD 17) # Specify the minimum version for creating a vector index. find_package(mongocxx 3.11.0 REQUIRED) find_package(bsoncxx REQUIRED) add_executable(query_quick_start vector_index.cpp ) target_link_libraries(query_quick_start PRIVATE mongo::mongocxx_shared) target_link_libraries(query_quick_start PRIVATE mongo::bsoncxx_shared) Define the index.
Create a file named
vector_index.cpp
. Copy and paste the following code into the file.vector_index.cpp1 2 3 4 5 6 7 8 9 using bsoncxx::builder::basic::kvp; 10 using bsoncxx::builder::basic::make_array; 11 using bsoncxx::builder::basic::make_document; 12 13 int main() { 14 try { 15 mongocxx::instance inst{}; 16 17 // Replace the placeholder with your Atlas connection string 18 const auto uri = mongocxx::uri{"<connection-string>"}; 19 20 // Connect to your Atlas cluster 21 mongocxx::client conn{uri}; 22 auto db = conn["sample_mflix"]; 23 auto collection = db["embedded_movies"]; 24 25 auto siv = collection.search_indexes(); 26 std::string name = "vector_index"; 27 auto type = "vectorSearch"; 28 auto definition = make_document( 29 kvp("fields", 30 make_array(make_document( 31 kvp("type", "vector"), kvp("path", "plot_embedding"), 32 kvp("numDimensions", 1536), kvp("similarity", "dotProduct"))))); 33 auto model = 34 mongocxx::search_index_model(name, definition.view()).type(type); 35 siv.create_one(model); 36 std::cout << "New search index named " << name << " is building." 37 << std::endl; 38 39 // Polling for index status 40 std::cout << "Polling to check if the index is ready. This may take up to " 41 "a minute." 42 << std::endl; 43 bool queryable = false; 44 while (!queryable) { 45 auto indexes = siv.list(); 46 for (const auto& index : indexes) { 47 if (index["name"].get_value() == name) { 48 queryable = index["queryable"].get_bool(); 49 } 50 } 51 if (!queryable) { 52 std::this_thread::sleep_for(std::chrono::seconds(5)); 53 } 54 } 55 std::cout << name << " is ready for querying." << std::endl; 56 } catch (const std::exception& e) { 57 std::cout << "Exception: " << e.what() << std::endl; 58 } 59 return 0; 60 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Create and enter the
/build
directory:mkdir build && cd build Prepare the project.
cmake ../ Build the app.
cmake --build . Execute the app to create the index.
./query_quick_start 1 New search index named vector_index is building. 2 Polling to check if the index is ready. This may take up to a minute. 3 vector_index is ready for querying.
When the sample data finishes loading, click Create Search Index.
Under the Atlas Vector Search section, select the JSON Editor and click Next.
In the Database and Collection section, expand the
sample_mflix
database and select theembedded_movies
collection.Each document in this collection contains information about a movie, including a summary of the movie's plot as a string, which has also been converted to and stored as a vector embedding in the document's
plot_embedding
field.In the Index Name field, specify
vector_index
.Copy and paste the following vector search index definition into the JSON Editor.
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Click Next.
Click Create Search Index.
The index should take about one minute to build. When your vector index is finished building, the Status column reads Active.
Initialize your Go module:
mkdir go-vector-quickstart && cd go-vector-quickstart go mod init go-vector-quickstart Add the Go Driver as a dependency in your project:
go get go.mongodb.org/mongo-driver/mongo For more detailed installation instructions, see the MongoDB Go Driver documentation.
Define the index.
Create a file named
vector-index.go
. Copy and paste the following code into the file.vector-index.go1 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 } 38 39 type vectorDefinition struct { 40 Fields []vectorDefinitionField `bson:"fields"` 41 } 42 43 indexName := "vector_index" 44 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 45 46 indexModel := mongo.SearchIndexModel{ 47 Definition: vectorDefinition{ 48 Fields: []vectorDefinitionField{{ 49 Type: "vector", 50 Path: "plot_embedding", 51 NumDimensions: 1536, 52 Similarity: "euclidean"}}, 53 }, 54 Options: opts, 55 } 56 57 // Create 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 log.Println("New search index named " + searchIndexName + " is building.") 63 64 // Await the creation of the index. 65 log.Println("Polling to check if the index is ready. 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(searchIndexName + " is ready for querying.") 88 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
go run vector-index.go 2024/10/17 09:38:21 New search index named vector_index is building. 2024/10/17 09:38:22 Polling to check if the index is ready. This may take up to a minute. 2024/10/17 09:38:48 vector_index is ready for querying.
Add the Java driver version 5.2 or higher as a dependency in your project:
If you are using Maven, add the following dependency to your
pom.xml
dependencies list:<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> If you are using Gradle, add the following dependency to your
build.gradle
dependencies list:dependencies { implementation 'org.mongodb:mongodb-driver-sync:[5.2.0,)' }
Add the Java driver JAR files to your
CLASSPATH
.For more detailed installation instructions and version compatibility, see the MongoDB Java Driver documentation.
Create a file named
VectorIndex.java
. Copy and paste the following code into the file.VectorIndex.java1 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", "euclidean"))); 38 39 // Define the index model 40 SearchIndexModel indexModel = new SearchIndexModel( 41 indexName, 42 definition, 43 SearchIndexType.vectorSearch()); 44 45 // Create the index 46 try { 47 List<String> result = collection.createSearchIndexes(Collections.singletonList(indexModel)); 48 System.out.println("New search index named " + result.get(0) + " is building."); 49 } catch (Exception e) { 50 throw new RuntimeException("Error creating index: " + e); 51 } 52 53 // Wait for Atlas to build the index 54 System.out.println("Polling to check if the index is ready. This may take up to a minute."); 55 56 ListSearchIndexesIterable<Document> searchIndexes = collection.listSearchIndexes(); 57 Document doc = null; 58 while (doc == null) { 59 try (MongoCursor<Document> cursor = searchIndexes.iterator()) { 60 if (!cursor.hasNext()) { 61 break; 62 } 63 Document current = cursor.next(); 64 String name = current.getString("name"); 65 // When the index completes building, it becomes `queryable` 66 boolean queryable = current.getBoolean("queryable"); 67 if (name.equals(indexName) && queryable) { 68 doc = current; 69 } else { 70 Thread.sleep(500); 71 } 72 } catch (Exception e) { 73 throw new RuntimeException("Failed to list search indexes: " + e); 74 } 75 } 76 System.out.println(indexName + " is ready for querying."); 77 78 } catch (Exception e) { 79 throw new RuntimeException("Error connecting to MongoDB: " + e); 80 } 81 } 82 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the file in your IDE, or execute a command from the command line to run the code.
javac VectorIndex.java java VectorIndex New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB Kotlin Coroutine Driver.
For more detailed installation instructions and version compatibility, see the MongoDB Kotlin Coroutine Driver documentation.
Define the index.
Create a file named
VectorIndex.kt
. Copy and paste the following code into the file.VectorIndex.kt1 import com.mongodb.MongoException 2 import com.mongodb.client.model.SearchIndexModel 3 import com.mongodb.client.model.SearchIndexType 4 import com.mongodb.kotlin.client.coroutine.MongoClient 5 import kotlinx.coroutines.delay 6 import kotlinx.coroutines.flow.toList 7 import org.bson.Document 8 import kotlinx.coroutines.runBlocking 9 10 fun main() { 11 // Replace the placeholder with your MongoDB deployment's connection string 12 val uri = "<connection-string>" 13 val mongoClient = MongoClient.create(uri) 14 val database = mongoClient.getDatabase("sample_mflix") 15 val collection = database.getCollection<Document>("embedded_movies") 16 val indexName = "vector_index" 17 val searchIndexModel = SearchIndexModel( 18 indexName, 19 Document( 20 "fields", 21 listOf( 22 Document("type", "vector") 23 .append("path", "plot_embedding") 24 .append("numDimensions", 1536) 25 .append("similarity", "dotProduct") 26 ) 27 ), 28 SearchIndexType.vectorSearch() 29 ) 30 31 runBlocking { 32 try { 33 collection.createSearchIndexes(listOf(searchIndexModel)) 34 .collect { result -> 35 println("New search index named $result is building.") 36 } 37 38 // Polling to check if the index is queryable 39 println("Polling to check if the index is ready. This may take up to a minute.") 40 var isQueryable = false 41 while (!isQueryable) { 42 delay(5000L) // Poll every 5 seconds 43 44 val indexes = collection.listSearchIndexes().toList() 45 isQueryable = indexes.any { index -> 46 index.getString("name") == indexName && index.getBoolean("queryable") 47 } 48 if (isQueryable) { 49 println("$indexName is ready for querying.") 50 } 51 } 52 } catch (me: MongoException) { 53 System.err.println("An error occurred: $me") 54 } 55 } 56 mongoClient.close() 57 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the
VectorIndex.kt
file in your IDE. The output should resemble the following:New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB Kotlin Sync Driver.
For more detailed installation instructions and version compatibility, see the MongoDB Kotlin Sync Driver documentation.
Define the index.
Create a file named
VectorIndex.kt
. Copy and paste the following code into the file.VectorIndex.kt1 import com.mongodb.MongoException 2 import com.mongodb.client.model.SearchIndexModel 3 import com.mongodb.client.model.SearchIndexType 4 import com.mongodb.kotlin.client.MongoClient 5 import org.bson.Document 6 7 fun main() { 8 // Replace the placeholder with your MongoDB deployment's connection string 9 val uri = "<connection-string>" 10 val mongoClient = MongoClient.create(uri) 11 val database = mongoClient.getDatabase("sample_mflix") 12 val collection = database.getCollection<Document>("embedded_movies") 13 val indexName = "vector_index" 14 val searchIndexModel = SearchIndexModel( 15 indexName, 16 Document( 17 "fields", 18 listOf( 19 Document("type", "vector") 20 .append("path", "plot_embedding") 21 .append("numDimensions", 1536) 22 .append("similarity", "dotProduct") 23 ) 24 ), 25 SearchIndexType.vectorSearch() 26 ) 27 28 try { 29 val result = collection.createSearchIndexes( 30 listOf(searchIndexModel) 31 ) 32 println("New search index named ${result.get(0)} is building.") 33 34 // Polling to check if the index is queryable 35 println("Polling to check if the index is ready. This may take up to a minute.") 36 var isQueryable = false 37 while (!isQueryable) { 38 val results = collection.listSearchIndexes() 39 results.forEach { result -> 40 if (result.getString("name") == indexName && result.getBoolean("queryable")) { 41 println("$indexName is ready for querying.") 42 isQueryable = true 43 } else { 44 Thread.sleep(5000) // Poll every 5 seconds 45 } 46 } 47 } 48 } catch (me: MongoException) { 49 System.err.println("An error occurred: $me") 50 } 51 mongoClient.close() 52 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the
VectorIndex.kt
file in your IDE. The output should resemble the following:New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Add the MongoDB Node Driver as a dependency in your project:
npm install mongodb Tip
The examples on this page assume your project manages modules as CommonJS modules. If you're using ES modules, instead, you must modify the import syntax.
Define the index.
Create a file named
vector-index.js
. Copy and paste the following code into the file.vector-index.js1 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": "euclidean" 24 } 25 ] 26 } 27 } 28 29 // run the helper method 30 const result = await collection.createSearchIndex(index); 31 console.log(`New search index named ${result} is building.`); 32 33 // wait for the index to be ready to query 34 console.log("Polling to check if the index is ready. This may take up to a minute.") 35 let isQueryable = false; 36 while (!isQueryable) { 37 const cursor = collection.listSearchIndexes(); 38 for await (const index of cursor) { 39 if (index.name === result) { 40 if (index.queryable) { 41 console.log(`${result} is ready for querying.`); 42 isQueryable = true; 43 } else { 44 await new Promise(resolve => setTimeout(resolve, 5000)); 45 } 46 } 47 } 48 } 49 } finally { 50 await client.close(); 51 } 52 } 53 run().catch(console.dir); This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
node vector-index.js New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB PHP Driver.
For detailed installation instructions, see the MongoDB PHP Library documentation.
Define the index.
Create a file named
vector-index.php
. Copy and paste the following code into the file.vector-index.php1 2 3 require 'vendor/autoload.php'; 4 5 // Replace the placeholder with your Atlas connection string 6 $uri = "<connection-string>"; 7 $client = new MongoDB\Client($uri); 8 $collection = $client->sample_mflix->embedded_movies; 9 $indexName = "vector_index"; 10 try { 11 $result = $collection->createSearchIndexes( 12 [[ 13 'name' => $indexName, 14 'type' => 'vectorSearch', 15 'definition' => [ 16 'fields' => [[ 17 'type' => 'vector', 18 'path' => 'plot_embedding', 19 'numDimensions' => 1536, 20 'similarity' => 'dotProduct' 21 ]] 22 ], 23 ]] 24 ); 25 echo "New search index named $result[0] is building." .PHP_EOL; 26 27 // Polling for the index to become queryable 28 echo "Polling to check if the index is ready. This may take up to a minute." .PHP_EOL; 29 $isIndexQueryable = false; 30 while (!$isIndexQueryable) { 31 // List the search indexes 32 $searchIndexes = $collection->listSearchIndexes(); 33 // Check if the index is present and queryable 34 foreach ($searchIndexes as $index) { 35 if ($index->name === $indexName) { 36 $isIndexQueryable = $index->queryable; 37 } 38 } 39 if (!$isIndexQueryable) { 40 sleep(5); // Wait for 5 seconds before polling again 41 } 42 } 43 echo "$indexName is ready for querying." .PHP_EOL; 44 } 45 catch (MongoDB\Driver\Exception\Exception $e) { 46 print 'Error creating the index: ' .$e->getMessage() .PHP_EOL; 47 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
php vector-index.php New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Add the PyMongo Driver as a dependency in your project:
pip install pymongo For more detailed installation instructions, see the MongoDB Python Driver documentation.
Define the index.
Create a file named
vector-index.py
. Copy and paste the following code into the file.vector-index.py1 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": "euclidean" 22 } 23 ] 24 }, 25 name="vector_index", 26 type="vectorSearch", 27 ) 28 29 result = collection.create_search_index(model=search_index_model) 30 print("New search index named " + result + " is building.") 31 # Wait for initial sync to complete 32 print("Polling to check if the index is ready. This may take up to a minute.") 33 predicate=None 34 if predicate is None: 35 predicate = lambda index: index.get("queryable") is True 36 37 while True: 38 indices = list(collection.list_search_indexes(name)) 39 if len(indices) and predicate(indices[0]): 40 break 41 time.sleep(5) 42 print(result + " is ready for querying.") 43 44 client.close() This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
python vector-index.py New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
When the sample data finishes loading, click Create Search Index.
Under the Atlas Vector Search section, select the JSON Editor and click Next.
In the Database and Collection section, expand the
sample_mflix
database and select theembedded_movies
collection.Each document in this collection contains information about a movie, including a summary of the movie's plot as a string, which has also been converted to and stored as a vector embedding in the document's
plot_embedding
field.In the Index Name field, specify
vector_index
.Copy and paste the following vector search index definition into the JSON Editor.
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Click Next.
Click Create Search Index.
The index should take about one minute to build. When your vector index is finished building, the Status column reads Active.
Install the Rust driver for MongoDB.
For more detailed installation instructions, see the MongoDB Rust Driver documentation.
Define the index.
In the
/src
directory of your project, create a file namedvector_index.rs
. Copy and paste the following code into the file.vector_index.rs1 use std::ops::Index; 2 use std::time::Duration; 3 use futures::{TryStreamExt}; 4 use mongodb::{bson::{Document, doc}, Client, Collection, SearchIndexModel}; 5 use mongodb::SearchIndexType::VectorSearch; 6 use tokio::time::sleep; 7 8 9 pub(crate) async fn vector_index() { 10 // Replace the placeholder with your Atlas connection string 11 let uri = "<connection_string>"; 12 13 // Create a new client and connect to the server 14 let client = Client::with_uri_str(uri).await.unwrap(); 15 16 // Get a handle on the movies collection 17 let database = client.database("sample_mflix"); 18 let my_coll: Collection<Document> = database.collection("embedded_movies"); 19 20 let index_name = "vector_index"; 21 let search_index_def = SearchIndexModel::builder() 22 .definition(doc! { 23 "fields": vec! {doc! { 24 "type": "vector", 25 "path": "plot_embedding", 26 "numDimensions": 1536, 27 "similarity": "dotProduct" 28 }} 29 }) 30 .name(index_name.to_string()) 31 .index_type(VectorSearch) 32 .build(); 33 34 let models = vec![search_index_def]; 35 let result = my_coll.create_search_indexes(models).await; 36 if let Err(e) = result { 37 eprintln!("There was an error creating the search index: {}", e); 38 std::process::exit(1) 39 } else { 40 println!("New search index named {} is building.", result.unwrap().index(0)); 41 } 42 43 // Polling for the index to become queryable 44 println!("Polling to check if the index is ready. This may take up to a minute."); 45 let mut is_index_queryable = false; 46 while !is_index_queryable { 47 // List the search indexes 48 let mut search_indexes = my_coll.list_search_indexes().await.unwrap(); 49 // Check if the index is present and queryable 50 while let Some(index) = search_indexes.try_next().await.unwrap() { 51 let retrieved_name = index.get_str("name"); 52 if retrieved_name.unwrap().to_string() == index_name { 53 is_index_queryable = index.get_bool("queryable").unwrap(); 54 } 55 } 56 if !is_index_queryable { 57 sleep(Duration::from_secs(5)).await; // Wait for 5 seconds before polling again 58 } 59 } 60 println!("{} is ready for querying.", index_name); 61 } vector_index.rs1 use std::ops::Index; 2 use std::time::Duration; 3 use std::thread::sleep; 4 use mongodb::{ 5 bson::{doc, Document}, 6 Client, Collection, SearchIndexModel, 7 }; 8 use mongodb::options::ClientOptions; 9 use mongodb::SearchIndexType::VectorSearch; 10 11 pub(crate) fn vector_index() { 12 // Replace the placeholder with your Atlas connection string 13 let uri = "<connection_string>"; 14 15 // Create a new client and connect to the server 16 let options = ClientOptions::parse(uri).run().unwrap(); 17 let client = Client::with_options(options).unwrap(); 18 19 // Get a handle on the movies collection 20 let database = client.database("sample_mflix"); 21 let my_coll: Collection<Document> = database.collection("embedded_movies"); 22 23 let index_name = "vector_index"; 24 let search_index_def = SearchIndexModel::builder() 25 .definition(doc! { 26 "fields": vec! {doc! { 27 "type": "vector", 28 "path": "plot_embedding", 29 "numDimensions": 1536, 30 "similarity": "dotProduct" 31 }} 32 }) 33 .name(index_name.to_string()) 34 .index_type(VectorSearch) 35 .build(); 36 37 let models = vec![search_index_def]; 38 let result = my_coll.create_search_indexes(models).run(); 39 if let Err(e) = result { 40 eprintln!("There was an error creating the search index: {}", e); 41 std::process::exit(1) 42 } else { 43 println!("New search index named {} is building.", result.unwrap().index(0)); 44 } 45 46 // Polling for the index to become queryable 47 println!("Polling to check if the index is ready. This may take up to a minute."); 48 let mut is_index_queryable = false; 49 while !is_index_queryable { 50 // List the search indexes 51 let search_indexes = my_coll.list_search_indexes().run().unwrap(); 52 53 // Check if the index is present and queryable 54 for index in search_indexes { 55 let unwrapped_index = index.unwrap(); 56 let retrieved_name = unwrapped_index.get_str("name").unwrap(); 57 if retrieved_name == index_name { 58 is_index_queryable = unwrapped_index.get_bool("queryable").unwrap_or(false); 59 } 60 } 61 62 if !is_index_queryable { 63 sleep(Duration::from_secs(5)); // Wait for 5 seconds before polling again 64 } 65 } 66 println!("{} is ready for querying.", index_name); 67 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Call the function from your
main.rs
.mod vector_index; fn main() { vector_index::vector_index(); } Run the file in your IDE, or execute a command from the command line to run the code.
cargo run New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB Scala Driver.
For installation instructions based on your environment and the version of Scala you are using, refer to the MongoDB Scala Driver documentation.
Create a new Scala project with the tools you normally use. For this quick start, we create a project named
quick-start
, by using sbt.sbt new scala/hello-world.g8 When prompted, name the application
quick-start
.Navigate to your
quick-start
project and create a file namedVectorIndex.scala
. Copy and paste the following code into the file.VectorIndex.scala1 import org.mongodb.scala._ 2 import org.mongodb.scala.model._ 3 import com.mongodb.client.model.SearchIndexType 4 5 class VectorIndex { 6 def createIndex(): Unit = { 7 val collection = 8 MongoClient("<connection-string>") 9 .getDatabase("sample_mflix") 10 .getCollection("embedded_movies") 11 val indexName = "vector_index" 12 val indexNameAsOption = Option(indexName) 13 val indexDefinition = Document( 14 "fields" -> List( 15 Document( 16 "type" -> "vector", 17 "path" -> "plot_embedding", 18 "numDimensions" -> 1536, 19 "similarity" -> "dotProduct" 20 ) 21 ) 22 ) 23 val indexType = Option(SearchIndexType.vectorSearch()) 24 val indexModel: SearchIndexModel = SearchIndexModel( 25 indexNameAsOption, indexDefinition, indexType 26 ) 27 collection.createSearchIndexes(List(indexModel)).foreach { doc => println(s"New search index named $doc is building.") } 28 Thread.sleep(2000) 29 println("Polling to check if the index is ready. This may take up to a minute.") 30 var indexReady = false 31 while (!indexReady) { 32 val searchIndexes = collection.listSearchIndexes() 33 searchIndexes.foreach { current => 34 val document = current.toBsonDocument() 35 val name = document.get("name").asString().getValue 36 val queryable = document.get("queryable").asBoolean().getValue 37 if (name == indexName && queryable) { 38 indexReady = true 39 } 40 } 41 if (!indexReady) { 42 Thread.sleep(5000) 43 } 44 } 45 println(s"$indexName is ready for querying.") 46 } 47 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Create a class instance and call the function in your project's
Main.scala
file.object Main extends App { private val indexInstance = new VectorIndex indexInstance.createIndex() } Run the file in your IDE, or execute a command from the command line to run the code.
There are many tools and environments in which Scala runs. In this example, we run the new Scala project by starting the sbt server with the
sbt
command, then typing~run
.sbt:quick-start> ~run New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the dependencies.
For detailed instructions, see Prerequisites.
Install the Atlas CLI.
If you use Homebrew, you can run the following command in your terminal:
brew install mongodb-atlas-cli For installation instructions on other operating systems, see Install the Atlas CLI
Install Docker.
Docker requires a network connection for pulling and caching MongoDB images.
For MacOS or Windows, install Docker Desktop v4.31+.
For Linux, install Docker Engine v27.0+.
For RHEL, you can also use Podman v5.0+.
Set up your local Atlas deployment.
If you don't have an existing Atlas account, run
atlas setup
in your terminal or create a new account.Run
atlas deployments setup
and follow the prompts to create a local deployment. When prompted to connect to the deployment, selectskip
.For detailed instructions, see Create a Local Atlas Deployment.
Load the sample data.
Run the following command in your terminal to download the sample data:
curl https://atlas-education.s3.amazonaws.com/sampledata.archive -o sampledata.archive Run the following command to load the data into your deployment, replacing
<port-number>
with the port where you're hosting the deployment:mongorestore --archive=sampledata.archive --port=<port-number>
Create a Vector Search index.
Create a file named
vector-index.json
Copy and paste the following index definition into the JSON file.
{ "database": "sample_mflix", "collectionName": "embedded_movies", "type": "vectorSearch", "name": "vector_index", "fields": [ { "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "dotProduct" } ] } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Save the file, and then run the following command in your terminal, replacing
<path-to-file>
with the path to thevector-index.json
file that you created.atlas deployments search indexes create --file <path-to-file>
Connect to the Atlas cluster using
mongosh
.In a terminal window, run
atlas deployments connect
and follow the prompts to connect to your local Atlas deployment viamongosh
. For detailed instructions on connecting, see Manage a Local Atlas Deployment.Switch to the database that contains the collection for which you want to create the index.
Example
use sample_mflix switched to db sample_mflix Run the
db.collection.createSearchIndex()
method.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": "euclidean" 11 } 12 ] 13 } 14 ); This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Install the MongoDB C Driver.
For detailed installation instructions, refer to the MongoDB C Driver documentation.
Create a new directory called
query-quick-start
.mkdir query-quick-start Enter the directory, and create a
CMakeLists.txt
file.cd query-quick-start touch CMakeLists.txt Copy and paste the following lines into the
CMakeLists.txt
file:cmake_minimum_required(VERSION 3.30) project(atlas-vector-search-quick-start) # Specify the minimum version for creating a vector index. find_package (mongoc-1.0 1.28.0 REQUIRED) add_executable(atlas-vector-search-quick-start vector_index.c ) target_link_libraries (atlas-vector-search-quick-start PRIVATE mongo::mongoc_shared) Define the index.
Create a file named
vector_index.c
. Copy and paste the following code into the file.vector_index.c1 2 3 4 5 int main(void) { 6 mongoc_client_t *client; 7 mongoc_collection_t *collection; 8 bson_error_t error; 9 char database_name[] = "sample_mflix"; 10 char collection_name[] = "embedded_movies"; 11 char index_name[] = "vector_index"; 12 13 mongoc_init(); 14 15 // Replace the placeholder with your Atlas connection string 16 client = mongoc_client_new("<connection-string>"); 17 18 // Connect to your Atlas cluster 19 collection = mongoc_client_get_collection (client, database_name, collection_name); 20 21 bson_t cmd; 22 // Create search index command. 23 { 24 char *cmd_str = bson_strdup_printf ( 25 BSON_STR ({ 26 "createSearchIndexes" : "%s", 27 "indexes" : [{ 28 "definition": { 29 "fields": [{ 30 "type": "vector", 31 "path": "plot_embedding", 32 "numDimensions": 1536, 33 "similarity": "dotProduct" 34 }] 35 }, 36 "name": "%s", 37 "type": "vectorSearch" 38 }] 39 }), 40 collection_name, index_name); 41 if (!bson_init_from_json(&cmd, cmd_str, -1, &error)) { 42 printf("Failed to initialize BSON: %s\n", error.message); 43 bson_free(cmd_str); 44 return 1; 45 } 46 bson_free (cmd_str); 47 } 48 if (!mongoc_collection_command_simple (collection, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { 49 bson_destroy (&cmd); 50 printf ("Failed to run createSearchIndexes: %s", error.message); 51 return 1; 52 } else { 53 printf ("New search index named %s is building.\n", index_name); 54 bson_destroy (&cmd); 55 } 56 57 // Polling for index status 58 printf("Polling to check if the index is ready. This may take up to a minute.\n"); 59 int queryable = 0; 60 while (!queryable) { 61 const char *pipeline_str = "{\"pipeline\": [{\"$listSearchIndexes\": {}}]}"; 62 bson_t pipeline; 63 if (!bson_init_from_json(&pipeline, pipeline_str, -1, &error)) { 64 printf("Failed to initialize pipeline BSON: %s\n", error.message); 65 break; // Exit the loop on error 66 } 67 mongoc_cursor_t *cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL); 68 const bson_t *got; 69 // Check if the cursor returns any documents 70 int found_index = 0; 71 while (mongoc_cursor_next(cursor, &got)) { 72 bson_iter_t iter; 73 if (bson_iter_init(&iter, got) && bson_iter_find(&iter, "name")) { 74 const char *name = bson_iter_utf8(&iter, NULL); 75 if (strcmp(name, index_name) == 0) { 76 found_index = 1; // Index found 77 bson_iter_find(&iter, "queryable"); 78 queryable = bson_iter_bool(&iter); 79 break; // Exit the loop since we found the index 80 } 81 } 82 } 83 if (mongoc_cursor_error(cursor, &error)) { 84 printf("Failed to run $listSearchIndexes: %s\n", error.message); 85 break; // Exit the loop on error 86 } 87 if (!found_index) { 88 printf("Index %s not found yet. Retrying...\n", index_name); 89 } 90 bson_destroy(&pipeline); 91 mongoc_cursor_destroy(cursor); 92 sleep(5); // Sleep for 5 seconds before checking again 93 } 94 if (queryable) { 95 printf("%s is ready for querying.\n", index_name); 96 } else { 97 printf("Error occurred or index not found.\n"); 98 } 99 100 // Cleanup 101 mongoc_collection_destroy(collection); 102 mongoc_client_destroy(client); 103 mongoc_cleanup(); 104 105 return 0; 106 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Create and enter the
/build
directory:mkdir build && cd build Prepare the project.
cmake ../ Build the app.
cmake --build . Execute the app to create the index.
./atlas-vector-search-quick-start 1 New search index named vector_index is building. 2 Polling to check if the index is ready. This may take up to a minute. 3 vector_index is ready for querying.
Install the MongoDB C++ Driver.
For detailed installation instructions, refer to the MongoDB C++ Driver documentation.
Create a new directory called
query-quick-start
.mkdir query-quick-start Enter the directory, and create a
CMakeLists.txt
file.cd query-quick-start touch CMakeLists.txt Copy and paste the following lines into the
CMakeLists.txt
file:cmake_minimum_required(VERSION 3.30) project(query_quick_start) set(CMAKE_CXX_STANDARD 17) # Specify the minimum version for creating a vector index. find_package(mongocxx 3.11.0 REQUIRED) find_package(bsoncxx REQUIRED) add_executable(query_quick_start vector_index.cpp ) target_link_libraries(query_quick_start PRIVATE mongo::mongocxx_shared) target_link_libraries(query_quick_start PRIVATE mongo::bsoncxx_shared) Define the index.
Create a file named
vector_index.cpp
. Copy and paste the following code into the file.vector_index.cpp1 2 3 4 5 6 7 8 9 using bsoncxx::builder::basic::kvp; 10 using bsoncxx::builder::basic::make_array; 11 using bsoncxx::builder::basic::make_document; 12 13 int main() { 14 try { 15 mongocxx::instance inst{}; 16 17 // Replace the placeholder with your Atlas connection string 18 const auto uri = mongocxx::uri{"<connection-string>"}; 19 20 // Connect to your Atlas cluster 21 mongocxx::client conn{uri}; 22 auto db = conn["sample_mflix"]; 23 auto collection = db["embedded_movies"]; 24 25 auto siv = collection.search_indexes(); 26 std::string name = "vector_index"; 27 auto type = "vectorSearch"; 28 auto definition = make_document( 29 kvp("fields", 30 make_array(make_document( 31 kvp("type", "vector"), kvp("path", "plot_embedding"), 32 kvp("numDimensions", 1536), kvp("similarity", "dotProduct"))))); 33 auto model = 34 mongocxx::search_index_model(name, definition.view()).type(type); 35 siv.create_one(model); 36 std::cout << "New search index named " << name << " is building." 37 << std::endl; 38 39 // Polling for index status 40 std::cout << "Polling to check if the index is ready. This may take up to " 41 "a minute." 42 << std::endl; 43 bool queryable = false; 44 while (!queryable) { 45 auto indexes = siv.list(); 46 for (const auto& index : indexes) { 47 if (index["name"].get_value() == name) { 48 queryable = index["queryable"].get_bool(); 49 } 50 } 51 if (!queryable) { 52 std::this_thread::sleep_for(std::chrono::seconds(5)); 53 } 54 } 55 std::cout << name << " is ready for querying." << std::endl; 56 } catch (const std::exception& e) { 57 std::cout << "Exception: " << e.what() << std::endl; 58 } 59 return 0; 60 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Create and enter the
/build
directory:mkdir build && cd build Prepare the project.
cmake ../ Build the app.
cmake --build . Execute the app to create the index.
./query_quick_start 1 New search index named vector_index is building. 2 Polling to check if the index is ready. This may take up to a minute. 3 vector_index is ready for querying.
Create a file named
vector-index.json
Copy and paste the following index definition into the JSON file.
{ "database": "sample_mflix", "collectionName": "embedded_movies", "type": "vectorSearch", "name": "vector_index", "fields": [ { "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "dotProduct" } ] } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
Save the file, and then run the following command in your terminal, replacing
<path-to-file>
with the path to thevector-index.json
file that you created.atlas deployments search indexes create --file <path-to-file>
Initialize your Go module:
mkdir go-vector-quickstart && cd go-vector-quickstart go mod init go-vector-quickstart Add the Go Driver as a dependency in your project:
go get go.mongodb.org/mongo-driver/mongo For more detailed installation instructions, see the MongoDB Go Driver documentation.
Define the index.
Create a file named
vector-index.go
. Copy and paste the following code into the file.vector-index.go1 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 } 38 39 type vectorDefinition struct { 40 Fields []vectorDefinitionField `bson:"fields"` 41 } 42 43 indexName := "vector_index" 44 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 45 46 indexModel := mongo.SearchIndexModel{ 47 Definition: vectorDefinition{ 48 Fields: []vectorDefinitionField{{ 49 Type: "vector", 50 Path: "plot_embedding", 51 NumDimensions: 1536, 52 Similarity: "euclidean"}}, 53 }, 54 Options: opts, 55 } 56 57 // Create 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 log.Println("New search index named " + searchIndexName + " is building.") 63 64 // Await the creation of the index. 65 log.Println("Polling to check if the index is ready. 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(searchIndexName + " is ready for querying.") 88 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
go run vector-index.go 2024/10/17 09:38:21 New search index named vector_index is building. 2024/10/17 09:38:22 Polling to check if the index is ready. This may take up to a minute. 2024/10/17 09:38:48 vector_index is ready for querying.
Add the Java driver version 5.2 or higher as a dependency in your project:
If you are using Maven, add the following dependency to your
pom.xml
dependencies list:<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> If you are using Gradle, add the following dependency to your
build.gradle
dependencies list:dependencies { implementation 'org.mongodb:mongodb-driver-sync:[5.2.0,)' }
Add the Java driver JAR files to your
CLASSPATH
.For more detailed installation instructions and version compatibility, see the MongoDB Java Driver documentation.
Create a file named
VectorIndex.java
. Copy and paste the following code into the file.VectorIndex.java1 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", "euclidean"))); 38 39 // Define the index model 40 SearchIndexModel indexModel = new SearchIndexModel( 41 indexName, 42 definition, 43 SearchIndexType.vectorSearch()); 44 45 // Create the index 46 try { 47 List<String> result = collection.createSearchIndexes(Collections.singletonList(indexModel)); 48 System.out.println("New search index named " + result.get(0) + " is building."); 49 } catch (Exception e) { 50 throw new RuntimeException("Error creating index: " + e); 51 } 52 53 // Wait for Atlas to build the index 54 System.out.println("Polling to check if the index is ready. This may take up to a minute."); 55 56 ListSearchIndexesIterable<Document> searchIndexes = collection.listSearchIndexes(); 57 Document doc = null; 58 while (doc == null) { 59 try (MongoCursor<Document> cursor = searchIndexes.iterator()) { 60 if (!cursor.hasNext()) { 61 break; 62 } 63 Document current = cursor.next(); 64 String name = current.getString("name"); 65 // When the index completes building, it becomes `queryable` 66 boolean queryable = current.getBoolean("queryable"); 67 if (name.equals(indexName) && queryable) { 68 doc = current; 69 } else { 70 Thread.sleep(500); 71 } 72 } catch (Exception e) { 73 throw new RuntimeException("Failed to list search indexes: " + e); 74 } 75 } 76 System.out.println(indexName + " is ready for querying."); 77 78 } catch (Exception e) { 79 throw new RuntimeException("Error connecting to MongoDB: " + e); 80 } 81 } 82 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the file in your IDE, or execute a command from the command line to run the code.
javac VectorIndex.java java VectorIndex New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB Kotlin Coroutine Driver.
For more detailed installation instructions and version compatibility, see the MongoDB Kotlin Coroutine Driver documentation.
Define the index.
Create a file named
VectorIndex.kt
. Copy and paste the following code into the file.VectorIndex.kt1 import com.mongodb.MongoException 2 import com.mongodb.client.model.SearchIndexModel 3 import com.mongodb.client.model.SearchIndexType 4 import com.mongodb.kotlin.client.coroutine.MongoClient 5 import kotlinx.coroutines.delay 6 import kotlinx.coroutines.flow.toList 7 import org.bson.Document 8 import kotlinx.coroutines.runBlocking 9 10 fun main() { 11 // Replace the placeholder with your MongoDB deployment's connection string 12 val uri = "<connection-string>" 13 val mongoClient = MongoClient.create(uri) 14 val database = mongoClient.getDatabase("sample_mflix") 15 val collection = database.getCollection<Document>("embedded_movies") 16 val indexName = "vector_index" 17 val searchIndexModel = SearchIndexModel( 18 indexName, 19 Document( 20 "fields", 21 listOf( 22 Document("type", "vector") 23 .append("path", "plot_embedding") 24 .append("numDimensions", 1536) 25 .append("similarity", "dotProduct") 26 ) 27 ), 28 SearchIndexType.vectorSearch() 29 ) 30 31 runBlocking { 32 try { 33 collection.createSearchIndexes(listOf(searchIndexModel)) 34 .collect { result -> 35 println("New search index named $result is building.") 36 } 37 38 // Polling to check if the index is queryable 39 println("Polling to check if the index is ready. This may take up to a minute.") 40 var isQueryable = false 41 while (!isQueryable) { 42 delay(5000L) // Poll every 5 seconds 43 44 val indexes = collection.listSearchIndexes().toList() 45 isQueryable = indexes.any { index -> 46 index.getString("name") == indexName && index.getBoolean("queryable") 47 } 48 if (isQueryable) { 49 println("$indexName is ready for querying.") 50 } 51 } 52 } catch (me: MongoException) { 53 System.err.println("An error occurred: $me") 54 } 55 } 56 mongoClient.close() 57 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the
VectorIndex.kt
file in your IDE. The output should resemble the following:New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB Kotlin Sync Driver.
For more detailed installation instructions and version compatibility, see the MongoDB Kotlin Sync Driver documentation.
Define the index.
Create a file named
VectorIndex.kt
. Copy and paste the following code into the file.VectorIndex.kt1 import com.mongodb.MongoException 2 import com.mongodb.client.model.SearchIndexModel 3 import com.mongodb.client.model.SearchIndexType 4 import com.mongodb.kotlin.client.MongoClient 5 import org.bson.Document 6 7 fun main() { 8 // Replace the placeholder with your MongoDB deployment's connection string 9 val uri = "<connection-string>" 10 val mongoClient = MongoClient.create(uri) 11 val database = mongoClient.getDatabase("sample_mflix") 12 val collection = database.getCollection<Document>("embedded_movies") 13 val indexName = "vector_index" 14 val searchIndexModel = SearchIndexModel( 15 indexName, 16 Document( 17 "fields", 18 listOf( 19 Document("type", "vector") 20 .append("path", "plot_embedding") 21 .append("numDimensions", 1536) 22 .append("similarity", "dotProduct") 23 ) 24 ), 25 SearchIndexType.vectorSearch() 26 ) 27 28 try { 29 val result = collection.createSearchIndexes( 30 listOf(searchIndexModel) 31 ) 32 println("New search index named ${result.get(0)} is building.") 33 34 // Polling to check if the index is queryable 35 println("Polling to check if the index is ready. This may take up to a minute.") 36 var isQueryable = false 37 while (!isQueryable) { 38 val results = collection.listSearchIndexes() 39 results.forEach { result -> 40 if (result.getString("name") == indexName && result.getBoolean("queryable")) { 41 println("$indexName is ready for querying.") 42 isQueryable = true 43 } else { 44 Thread.sleep(5000) // Poll every 5 seconds 45 } 46 } 47 } 48 } catch (me: MongoException) { 49 System.err.println("An error occurred: $me") 50 } 51 mongoClient.close() 52 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the
VectorIndex.kt
file in your IDE. The output should resemble the following:New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Add the MongoDB Node Driver as a dependency in your project:
npm install mongodb Tip
The examples on this page assume your project manages modules as CommonJS modules. If you're using ES modules, instead, you must modify the import syntax.
Define the index.
Create a file named
vector-index.js
. Copy and paste the following code into the file.vector-index.js1 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": "euclidean" 24 } 25 ] 26 } 27 } 28 29 // run the helper method 30 const result = await collection.createSearchIndex(index); 31 console.log(`New search index named ${result} is building.`); 32 33 // wait for the index to be ready to query 34 console.log("Polling to check if the index is ready. This may take up to a minute.") 35 let isQueryable = false; 36 while (!isQueryable) { 37 const cursor = collection.listSearchIndexes(); 38 for await (const index of cursor) { 39 if (index.name === result) { 40 if (index.queryable) { 41 console.log(`${result} is ready for querying.`); 42 isQueryable = true; 43 } else { 44 await new Promise(resolve => setTimeout(resolve, 5000)); 45 } 46 } 47 } 48 } 49 } finally { 50 await client.close(); 51 } 52 } 53 run().catch(console.dir); This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
node vector-index.js New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Install the MongoDB PHP Driver.
For detailed installation instructions, see the MongoDB PHP Library documentation.
Define the index.
Create a file named
vector-index.php
. Copy and paste the following code into the file.vector-index.php1 2 3 require 'vendor/autoload.php'; 4 5 // Replace the placeholder with your Atlas connection string 6 $uri = "<connection-string>"; 7 $client = new MongoDB\Client($uri); 8 $collection = $client->sample_mflix->embedded_movies; 9 $indexName = "vector_index"; 10 try { 11 $result = $collection->createSearchIndexes( 12 [[ 13 'name' => $indexName, 14 'type' => 'vectorSearch', 15 'definition' => [ 16 'fields' => [[ 17 'type' => 'vector', 18 'path' => 'plot_embedding', 19 'numDimensions' => 1536, 20 'similarity' => 'dotProduct' 21 ]] 22 ], 23 ]] 24 ); 25 echo "New search index named $result[0] is building." .PHP_EOL; 26 27 // Polling for the index to become queryable 28 echo "Polling to check if the index is ready. This may take up to a minute." .PHP_EOL; 29 $isIndexQueryable = false; 30 while (!$isIndexQueryable) { 31 // List the search indexes 32 $searchIndexes = $collection->listSearchIndexes(); 33 // Check if the index is present and queryable 34 foreach ($searchIndexes as $index) { 35 if ($index->name === $indexName) { 36 $isIndexQueryable = $index->queryable; 37 } 38 } 39 if (!$isIndexQueryable) { 40 sleep(5); // Wait for 5 seconds before polling again 41 } 42 } 43 echo "$indexName is ready for querying." .PHP_EOL; 44 } 45 catch (MongoDB\Driver\Exception\Exception $e) { 46 print 'Error creating the index: ' .$e->getMessage() .PHP_EOL; 47 } This index definition:
Indexes the
plot_embedding
field as thevector
type. This field contains vector embeddings that represent the summary of a movie's plot.Specifies
1536
vector dimensions.Measures similarity using
dotProduct
similarity.
This code also includes a polling mechanism to check if the index is ready to use.
Specify the
<connection-string>
.Replace
<connection-string>
with the connection string for your Atlas cluster or local deployment, and then save the file.Your connection string should use the following format:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net Note
Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers.
Your connection string should use the following format:
mongodb://localhost:<port-number>/?directConnection=true Run the following command to create the index.
php vector-index.php New search index named vector_index is building. Polling to check if the index is ready. This may take up to a minute. vector_index is ready for querying.
Add the PyMongo Driver as a dependency in your project:
pip install pymongo For more detailed installation instructions, see the MongoDB Python Driver documentation.
Define the index.
Create a file named
vector-index.py
. Copy and paste the following code into the file.vector-index.py1 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(