Overview
在本指南中,您可以学习如何创建和管理MongoDB搜索和MongoDB Vector搜索搜索索引。这些索引允许您使用以下功能:
MongoDB Search:执行快速全文搜索
MongoDB Vector Search:对向量嵌入执行语义(相似度)搜索
MongoDB Search 和 MongoDB Vector Search 搜索索引指定要索引的字段、索引这些字段的方式,以及设立其他可选配置。
注意
MongoDB 搜索索引管理方法异步运行。驱动程序方法可以在服务器上完成所需操作之前返回结果。
本指南介绍如何执行以下操作来管理MongoDB 搜索和MongoDB Vector 搜索索引:
注意
样本数据
本指南中的示例使用 sample_mflix数据库中的 embedded_movies集合,该数据库是Atlas示例数据集之一。有关导入Atlas示例数据的说明,请参阅Atlas文档中的加载样本数据。
创建搜索索引模型
要创建MongoDB Search 或MongoDB 向量搜索索引,您必须首先构建一个用于设置索引规范的 CreateSearchIndexModel实例。
CreateSearchIndexModel 具有以下属性:
属性 | 类型 | 说明 |
|---|---|---|
|
| 指定索引定义。如果省略此设置,驱动程序将创建具有动态映射的MongoDB搜索索引。 |
|
| 设置索引名称。如果省略此设置,驱动程序会将名称设置为 |
|
| 设置索引类型。如果省略此设置,驱动程序将默认创建MongoDB搜索索引。 |
要学习;了解有关MongoDB Search字段映射的更多信息,请参阅Atlas文档中的定义字段映射。
要了解有关定义 MongoDB 向量搜索 搜索索引的更多信息,请参阅 Atlas 文档中的 如何为向量搜索字段编制索引。
示例模型
以下示例创建一个 CreateSearchIndexModel实例,为名为 search_idx 的索引提供规范。该代码指定了 title 和 released 字段的静态映射:
var def = new BsonDocument { { "mappings", new BsonDocument { { "dynamic", false }, { "fields", new BsonDocument { { "title", new BsonDocument { {"type", "string" } } }, { "released", new BsonDocument { { "type", "date" } } } } } } } }; var indexModel = new CreateSearchIndexModel( "search_idx", SearchIndexType.Search, def );
以下示例创建一个 CreateSearchIndexModel 实例,以便为名为 vs_idx 的索引提供规范。代码将嵌入路径指定为 plot_embedding,索引 1536 个维度,并使用 "euclidean" 向量相似性函数:
var def = new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "plot_embedding" }, { "numDimensions", 1536 }, { "similarity", "euclidean" } } } } }; var indexModel = new CreateSearchIndexModel( "vs_idx", SearchIndexType.VectorSearch, def );
创建搜索索引
You can create a MongoDB Search or MongoDB Vector Search index on a collection by calling the SearchIndexes.CreateOne() method on an IMongoCollection instance. This method accepts an index model as a parameter, specified in a CreateSearchIndexModel instance.
例子
以下示例在 embedded_movies集合上创建MongoDB搜索索引。该代码创建一个 CreateSearchIndexModel,用于设置索引名称并启用动态映射。然后,代码将 CreateSearchIndexModel 实例传递给 SearchIndexes.CreateOne() 方法以创建 MongoDB 搜索索引:
var indexModel = new CreateSearchIndexModel( "example_index", SearchIndexType.Search, new BsonDocument { { "mappings", new BsonDocument { { "dynamic", true }, } } } ); var result = movieCollection.SearchIndexes.CreateOne(indexModel); Console.WriteLine("Created MongoDB Search index:\n{0}", result);
Created MongoDB Search index: "example_index"
创建多个搜索索引
您可以通过在 IMongoCollection 实例上调用 SearchIndexes.CreateMany() 方法来创建多个 MongoDB 搜索搜索索引和 MongoDB Vector 搜索搜索索引。此方法接受 CreateSearchIndexModel 实例的 IEnumerable 作为参数。
例子
此示例将执行以下动作:
创建一个
CreateSearchIndexModel实例,该实例指定名为as_idx的 MongoDB搜索索引创建一个
CreateSearchIndexModel实例,该实例指定名为vs_idx的MongoDB Vector Search索引将两个
CreateSearchIndexModel实例的List传递给SearchIndexes.CreateMany()方法在
embedded_movies集合上创建 MongoDB 搜索搜索索引 和 MongoDB Vector 搜索搜索索引
var searchModel = new CreateSearchIndexModel( "as_idx", SearchIndexType.Search, new BsonDocument { { "mappings", new BsonDocument { { "dynamic", true }, } } } ); var vectorModel = new CreateSearchIndexModel( "vs_idx", SearchIndexType.VectorSearch, new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "plot_embedding" }, { "numDimensions", 1536 }, { "similarity", "euclidean" } } } } } ); var models = new List<CreateSearchIndexModel> { searchModel, vectorModel }; var indexes = movieCollection.SearchIndexes.CreateMany(models); Console.WriteLine("Created Search indexes:\n{0} {1}", indexes.ToArray());
Created Search indexes: as_idx vs_idx
搜索索引列表
您可以通过调用集合上的 SearchIndexes.List() 方法来访问有关集合现有的 MongoDB 搜索和 MongoDB 向量搜索搜索索引的信息。
例子
以下示例访问有关在本页创建多个搜索索引部分中创建的MongoDB搜索和MongoDB Vector搜索索引的信息。该代码调用 SearchIndexes.List() 方法并打印集合上的 MongoDB 搜索 和 MongoDB 向量搜索 搜索索引的列表:
var indexesList = movieCollection.SearchIndexes.List().ToList(); foreach (var i in indexesList) { Console.WriteLine(i); }
{ "id": "...", "name": "as_idx", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, "latestDefinition": { "mappings": { "dynamic": true } }, "statusDetail": [...] } { "id": "...", "name": "vs_idx", "type": "vectorSearch", "status": "READY", "queryable": true, ..., "latestDefinition": { "fields": [{ "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "euclidean" }] }, "statusDetail": [...] }
更新搜索索引
You can update a MongoDB Search or MongoDB Vector Search index by calling the SearchIndexes.Update() method on an IMongoCollection instance. This method accepts the following parameters:
要更新的索引名称
修改索引定义文档
例子
以下示例更新了在本页 创建多个搜索索引部分中创建的名为 vs_index 的 Vector Search 索引。该代码创建了一个新的索引定义文档,指示索引使用 "dotProduct" 作为向量相似性函数。然后,代码调用 SearchIndexes.Update() 方法来更新索引:
var updatedDef = new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "plot_embedding" }, { "numDimensions", 1536 }, { "similarity", "dotProduct" } } } } }; movieCollection.SearchIndexes.Update("vs_index", updatedDef);
删除搜索索引
您可以通过在 IMongoCollection 实例上调用 SearchIndexes.DropOne() 方法来删除 MongoDB 搜索或 MongoDB 向量搜索索引。此方法接受要删除的索引名称作为参数。
例子
以下示例删除了在此页面的创建搜索索引部分中创建的名为 example_index 的MongoDB搜索索引。代码将索引名称传递给 SearchIndexes.DropOne() 方法以删除索引:
movieCollection.SearchIndexes.DropOne("example_index");
更多信息
要学习;了解可以使用.NET/ C#驱动程序创建的其他索引,请参阅创建和管理索引指南。
要学习;了解有关MongoDB Search 的更多信息,请参阅以下Atlas文档:
要学习;了解有关MongoDB Vector Search 的更多信息,请参阅以下Atlas文档:
API 文档
要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: