Atlas Vector Search 快速入门
本快速入门介绍了如何将包含向量嵌入的示例文档加载到Atlas 集群或本地 Atlas 部署中,在这些嵌入上创建 Atlas Vector Search 索引,然后执行语义搜索以返回与查询相似的文档。
所需时间:15 分钟
目标
在本快速入门中,您将执行以下步骤:
为
sample_mflix.embedded_movies
集合创建索引定义,该定义将plot_embedding
字段作为vector
类型编制索引。plot_embedding
字段包含使用 OpenAI 的text-embedding-ada-002
嵌入模型创建的嵌入。索引定义指定1536
个向量维度,并使用dotProduct
衡量相似性。运行 Atlas Vector Search 查询,搜索示例
sample_mflix.embedded_movies
集合。查询使用$vectorSearch
阶段搜索plot_embedding
字段,其中包含使用 OpenAI 的text-embedding-ada-002
嵌入模型创建的嵌入。该查询使用向量嵌入来搜索plot_embedding
字段中的字符串 time travel。它最多考虑150
个最近邻,并在结果中返回10
个文档。
要了解更多信息,请参阅学习总结。
创建 Vector Search 索引
➤ 要设置用于运行此页面上的示例的客户端,请使用右侧导航窗格中的选择语言下拉菜单。
在本部分中,您将在加载到 Atlas 集群或本地计算机托管部署中的示例数据上,创建 Atlas Vector Search 索引:
设置您的 Atlas 集群。
如果您还没有 Atlas 集群,可以免费创建一个 M0 集群。要了解有关创建 Atlas 集群的更多信息,请参阅创建集群。
注意
如果使用现有集群,则您必须拥有 Atlas 项目的
Project Data Access Admin
或更高访问权限。如果创建新集群,则默认拥有必要的权限。
每个项目只能创建一个
M0
免费集群。在左侧边栏,单击Atlas Search。从 Select data source 菜单中选择您的集群,然后单击 Go to Atlas Search。
如果尚未将样本数据集加载到集群,请单击 Load a Sample Dataset。在 Load Sample Dataset(加载样本数据集)对话框中,单击 Load Sample Dataset (确认)进行确认。
如果您已加载示例数据集,请检查并确认
sample_mflix
数据库包含embedded_movies
集合。如果没有,请删除样本数据库并重新加载样本数据集。加载样本数据集可能需要几分钟才能完成。
创建 Vector Search 索引。
注意
您可以使用mongosh
命令或驾驶员助手方法在所有AtlasAtlas Search Atlas集群层上创建Atlas Search索引。有关支持的驾驶员版本的列表,请参阅支持的客户端。
示例数据加载完成后,单击 Create Search Index。
在 Atlas Vector Search 部分中,选择 JSON Editor 并点击 Next。
在 Database and Collection 部分中,展开
sample_mflix
数据库,然后选择embedded_movies
集合。此集合中的每个文档 都包含有关电影的信息,包括以字符串形式显示的电影情节摘要,该摘要也已转换为嵌入到文档的
plot_embedding
字段中的矢量并存储为矢量。在 Index Name 字段中指定
vector_index
。将以下向量搜索索引定义复制并粘贴到 JSON 编辑器中。
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 } 此索引定义:
单击 Next(连接)。
单击 Create Search Index(连接)。
构建索引大约需要一分钟时间。当向量索引构建完成后,Status 列显示 Active。
使用
mongosh
连接到 Atlas 集群。在终端窗口中打开
mongosh
并连接到 Atlas 集群。有关连接的详细说明,请参阅通过 mongosh 连接。切换到包含要创建索引的集合的数据库。
例子
use sample_mflix switched to db sample_mflix 运行
db.collection.createSearchIndex()
方法。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 ); 此索引定义:
安装 MongoDB C 驱动程序。
有关详细安装说明,请参阅 MongoDB C 驱动程序文档。
创建一个名为
query-quick-start
的新目录。mkdir query-quick-start 进入目录,创建
CMakeLists.txt
文件。cd query-quick-start touch CMakeLists.txt 将以下行复制并粘贴到
CMakeLists.txt
文件: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) 定义索引。
创建一个名为
vector_index.c
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 创建并进入
/build
目录:mkdir build && cd build 请准备项目。
cmake ../ 构建应用程序。
cmake --build . 执行应用以创建索引。
./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.
安装MongoDB C++驱动程序。
有关详细安装说明,请参阅 MongoDB C++ 驱动程序文档。
创建一个名为
query-quick-start
的新目录。mkdir query-quick-start 进入目录,创建
CMakeLists.txt
文件。cd query-quick-start touch CMakeLists.txt 将以下行复制并粘贴到
CMakeLists.txt
文件: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) 定义索引。
创建一个名为
vector_index.cpp
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 创建并进入
/build
目录:mkdir build && cd build 请准备项目。
cmake ../ 构建应用程序。
cmake --build . 执行应用以创建索引。
./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 Search Index。
在 Atlas Vector Search 部分中,选择 JSON Editor 并点击 Next。
在 Database and Collection 部分中,展开
sample_mflix
数据库,然后选择embedded_movies
集合。此集合中的每个文档 都包含有关电影的信息,包括以字符串形式显示的电影情节摘要,该摘要也已转换为嵌入到文档的
plot_embedding
字段中的矢量并存储为矢量。在 Index Name 字段中指定
vector_index
。将以下向量搜索索引定义复制并粘贴到 JSON 编辑器中。
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 } 此索引定义:
单击 Next(连接)。
单击 Create Search Index(连接)。
构建索引大约需要一分钟时间。当向量索引构建完成后,Status 列显示 Active。
初始化您的 Go 模块:
mkdir go-vector-quickstart && cd go-vector-quickstart go mod init go-vector-quickstart 将 Go 驱动程序作为依赖项添加到项目中:
go get go.mongodb.org/mongo-driver/mongo 有关更详细的安装说明,请参阅 MongoDB Go 驱动程序文档。
定义索引。
创建一个名为
vector-index.go
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 运行以下命令以创建索引。
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.
将 Java 驱动程序版本 5.2 或更高版本作为依赖项添加到项目中:
如果您使用 Maven,请将以下依赖项添加到
pom.xml
依赖项列表中:<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> 如果您使用 Gradle,请将以下依赖项添加到
build.gradle
依赖项列表中:dependencies { implementation 'org.mongodb:mongodb-driver-sync:[5.2.0,)' }
将 Java 驱动程序的 JAR 文件添加到您的
CLASSPATH
中。有关更详细的安装说明和版本兼容性信息,请参阅 MongoDB Java 驱动程序文档。
创建一个名为
VectorIndex.java
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 在 IDE 中运行该文件,或从命令行执行命令来运行代码。
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.
安装 MongoDB Kotlin 协程驱动程序。
有关更详细的安装说明和版本兼容性信息,请参阅 MongoDB Kotlin 协程驱动程序文档。
定义索引。
创建一个名为
VectorIndex.kt
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 在 IDE 中运行
VectorIndex.kt
文件。输出应类似于以下内容: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.
安装 MongoDB Kotlin 同步驱动程序。
有关更详细的安装说明和版本兼容性,请参阅 MongoDB Kotlin Sync 驱动程序文档。
定义索引。
创建一个名为
VectorIndex.kt
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 在 IDE 中运行
VectorIndex.kt
文件。输出应类似于以下内容: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.
将 MongoDB Node 驱动程序作为依赖项添加到项目中:
npm install mongodb 提示
本页上的示例假设您的项目将模块作为 CommonJS 模块进行管理。而如果您使用的是 ES 模块,则必须修改导入语法。
定义索引。
创建一个名为
vector-index.js
的文件。将以下代码复制并粘贴到文件中。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); 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 运行以下命令以创建索引。
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.
安装 MongoDB PHP 驱动程序。
有关详细安装说明,请参阅MongoDB PHP 库文档。
定义索引。
创建一个名为
vector-index.php
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 运行以下命令以创建索引。
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.
将 PyMongo 驱动程序作为依赖项添加到项目中:
pip install pymongo 有关更详细的安装说明,请参阅 MongoDB Python 驱动程序文档。
定义索引。
创建一个名为
vector-index.py
的文件。将以下代码复制并粘贴到文件中。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() 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 运行以下命令以创建索引。
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.
示例数据加载完成后,单击 Create Search Index。
在 Atlas Vector Search 部分中,选择 JSON Editor 并点击 Next。
在 Database and Collection 部分中,展开
sample_mflix
数据库,然后选择embedded_movies
集合。此集合中的每个文档 都包含有关电影的信息,包括以字符串形式显示的电影情节摘要,该摘要也已转换为嵌入到文档的
plot_embedding
字段中的矢量并存储为矢量。在 Index Name 字段中指定
vector_index
。将以下向量搜索索引定义复制并粘贴到 JSON 编辑器中。
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "dotProduct" 7 }] 8 } 此索引定义:
单击 Next(连接)。
单击 Create Search Index(连接)。
构建索引大约需要一分钟时间。当向量索引构建完成后,Status 列显示 Active。
安装适用于 MongoDB 的 Rust 驱动程序。
有关更详细的安装说明,请参阅MongoDB Rust 驱动程序文档。
定义索引。
在项目的
/src
目录中,创建一个名为vector_index.rs
的文件。将以下代码复制并粘贴到该文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 从
main.rs
中调用该函数。mod vector_index; fn main() { vector_index::vector_index(); } 在 IDE 中运行该文件,或从命令行执行命令来运行代码。
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.
安装 MongoDB Scala 驱动程序。
有关基于环境和所使用的 Scala 版本的安装说明,请参阅 MongoDB Scala 驱动程序文档。
使用您常用的工具创建一个新的Scala项目。在本快速入门中,我们使用
quick-start
sbt 创建一个名为 的项目。sbt new scala/hello-world.g8 出现提示时,将应用程序命名为
quick-start
。导航到
quick-start
项目并创建一个名为VectorIndex.scala
的文件。将以下代码复制并粘贴到该文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 创建一个类实例并调用项目的
Main.scala
文件中的函数。object Main extends App { private val indexInstance = new VectorIndex indexInstance.createIndex() } 在 IDE 中运行该文件,或从命令行执行命令来运行代码。
Scala 可以在多种工具和环境中运行。在此示例中,我们通过使用
sbt
命令启动 sbt 服务器,然后键入~run
来运行新的 Scala 项目。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.
安装依赖项。
有关详细说明,请参阅先决条件。
如果使用 Homebrew ,则可以在终端中运行以下命令:
brew install mongodb-atlas-cli 安装 Docker。
Docker需要网络连接来拉取和缓存MongoDB映像。
对于 macOS 或 Windows,请安装 Docker 桌面 v4.31+。
对于 Linux,请安装 Docker Engine v27.0+。
对于 RHEL,您还可以使用 Podman v5.0 +。
设置本地 Atlas 部署。
如果没有 Atlas 账户,请在终端运行
atlas setup
或创建新帐户。运行
atlas deployments setup
并按照提示创建本地部署。当系统提示连接到部署时,选择skip
。有关详细说明,请参阅创建本地 Atlas 部署。
创建 Vector Search 索引。
创建一个文件,名为:
vector-index.json
将以下索引定义复制并粘贴到 JSON 文件中。
{ "database": "sample_mflix", "collectionName": "embedded_movies", "type": "vectorSearch", "name": "vector_index", "fields": [ { "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "dotProduct" } ] } 此索引定义:
保存文件,然后在终端中运行以下命令,将
<path-to-file>
替换为您创建的vector-index.json
文件的路径。atlas deployments search indexes create --file <path-to-file>
使用
mongosh
连接到 Atlas 集群。在终端窗口中,运行
atlas deployments connect
并按照提示通过mongosh
连接到本地 Atlas 部署。有关连接的详细说明,请参阅管理本地 Atlas 部署。切换到包含要创建索引的集合的数据库。
例子
use sample_mflix switched to db sample_mflix 运行
db.collection.createSearchIndex()
方法。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 ); 此索引定义:
安装 MongoDB C 驱动程序。
有关详细安装说明,请参阅 MongoDB C 驱动程序文档。
创建一个名为
query-quick-start
的新目录。mkdir query-quick-start 进入目录,创建
CMakeLists.txt
文件。cd query-quick-start touch CMakeLists.txt 将以下行复制并粘贴到
CMakeLists.txt
文件: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) 定义索引。
创建一个名为
vector_index.c
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 创建并进入
/build
目录:mkdir build && cd build 请准备项目。
cmake ../ 构建应用程序。
cmake --build . 执行应用以创建索引。
./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.
安装MongoDB C++驱动程序。
有关详细安装说明,请参阅 MongoDB C++ 驱动程序文档。
创建一个名为
query-quick-start
的新目录。mkdir query-quick-start 进入目录,创建
CMakeLists.txt
文件。cd query-quick-start touch CMakeLists.txt 将以下行复制并粘贴到
CMakeLists.txt
文件: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) 定义索引。
创建一个名为
vector_index.cpp
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 创建并进入
/build
目录:mkdir build && cd build 请准备项目。
cmake ../ 构建应用程序。
cmake --build . 执行应用以创建索引。
./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.
创建一个文件,名为:
vector-index.json
将以下索引定义复制并粘贴到 JSON 文件中。
{ "database": "sample_mflix", "collectionName": "embedded_movies", "type": "vectorSearch", "name": "vector_index", "fields": [ { "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "dotProduct" } ] } 此索引定义:
保存文件,然后在终端中运行以下命令,将
<path-to-file>
替换为您创建的vector-index.json
文件的路径。atlas deployments search indexes create --file <path-to-file>
初始化您的 Go 模块:
mkdir go-vector-quickstart && cd go-vector-quickstart go mod init go-vector-quickstart 将 Go 驱动程序作为依赖项添加到项目中:
go get go.mongodb.org/mongo-driver/mongo 有关更详细的安装说明,请参阅 MongoDB Go 驱动程序文档。
定义索引。
创建一个名为
vector-index.go
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 运行以下命令以创建索引。
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.
将 Java 驱动程序版本 5.2 或更高版本作为依赖项添加到项目中:
如果您使用 Maven,请将以下依赖项添加到
pom.xml
依赖项列表中:<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> 如果您使用 Gradle,请将以下依赖项添加到
build.gradle
依赖项列表中:dependencies { implementation 'org.mongodb:mongodb-driver-sync:[5.2.0,)' }
将 Java 驱动程序的 JAR 文件添加到您的
CLASSPATH
中。有关更详细的安装说明和版本兼容性信息,请参阅 MongoDB Java 驱动程序文档。
创建一个名为
VectorIndex.java
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 在 IDE 中运行该文件,或从命令行执行命令来运行代码。
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.
安装 MongoDB Kotlin 协程驱动程序。
有关更详细的安装说明和版本兼容性信息,请参阅 MongoDB Kotlin 协程驱动程序文档。
定义索引。
创建一个名为
VectorIndex.kt
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 在 IDE 中运行
VectorIndex.kt
文件。输出应类似于以下内容: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.
安装 MongoDB Kotlin 同步驱动程序。
有关更详细的安装说明和版本兼容性,请参阅 MongoDB Kotlin Sync 驱动程序文档。
定义索引。
创建一个名为
VectorIndex.kt
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 在 IDE 中运行
VectorIndex.kt
文件。输出应类似于以下内容: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.
将 MongoDB Node 驱动程序作为依赖项添加到项目中:
npm install mongodb 提示
本页上的示例假设您的项目将模块作为 CommonJS 模块进行管理。而如果您使用的是 ES 模块,则必须修改导入语法。
定义索引。
创建一个名为
vector-index.js
的文件。将以下代码复制并粘贴到文件中。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); 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true 运行以下命令以创建索引。
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.
安装 MongoDB PHP 驱动程序。
有关详细安装说明,请参阅MongoDB PHP 库文档。
定义索引。
创建一个名为
vector-index.php
的文件。将以下代码复制并粘贴到文件中。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 } 此索引定义:
此代码还包括一个轮询机制,用于检查索引是否已准备好使用。
指定
<connection-string>
(代表的电子邮件地址)。将
<connection-string>
替换为 Atlas 集群或本地部署的连接字符串,然后保存文件。连接字符串应使用以下格式:
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
确保连接字符串包含数据库用户的档案。要了解有关查找连接字符串的更多信息,请参阅通过驱动程序连接。
连接字符串应使用以下格式:
mongodb://localhost:<port-number>/?directConnection=true