对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB 搜索 和 MongoDB Vector 搜索索引

在本指南中,您可以学习如何创建和管理MongoDB搜索和MongoDB Vector搜索搜索索引。这些索引允许您使用以下功能:

MongoDB Search 和 MongoDB Vector Search 搜索索引指定要索引的字段、索引这些字段的方式,以及设立其他可选配置。

注意

MongoDB 搜索索引管理方法异步运行。驱动程序方法可以在服务器上完成所需操作之前返回结果。

本指南介绍如何执行以下操作来管理MongoDB 搜索和MongoDB Vector 搜索索引:

注意

样本数据

本指南中的示例使用 sample_mflix数据库中的 embedded_movies集合,该数据库是Atlas示例数据集之一。有关导入Atlas示例数据的说明,请参阅Atlas文档中的加载样本数据。

以下部分介绍如何为MongoDB Search 和MongoDB Vector Search 索引创建索引模型。

要创建MongoDB Search索引,您必须构造一个设置索引规范的 CreateSearchIndexModel实例。

CreateSearchIndexModel 类具有以下属性:

属性
类型
说明

Definition

BsonDocument

指定索引定义。如果省略此设置,驱动程序将创建具有动态映射的MongoDB搜索索引。

Name

string

设置索引名称。如果省略此设置,驱动程序会将名称设置为 default。

Type

SearchIndexType

设置索引类型。如果省略此设置,驱动程序将默认创建MongoDB搜索索引。

以下示例创建一个 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
);

要学习;了解有关MongoDB Search字段映射的更多信息,请参阅Atlas文档中的定义字段映射。

要创建MongoDB Vector Search索引,您必须构造一个设置索引规范的 CreateVectorSearchIndexModel实例。

CreateVectorSearchIndexModel 类继承 CreateSearchIndexModel 类,并具有以下附加属性:

属性
类型
说明

Field

FieldDefinition<TDocument>

指定包含要索引的向量的字段。

Similarity

VectorSimilarity

设置用于搜索前 K 个最近邻域的向量相似度函数。

Dimensions

int

指定搜索在索引时和查询时强制执行的维数。

FilterFields

IReadOnlyList<FieldDefinition<TDocument>>

指定搜索在向量查询中用作过滤器的字段。

Quantization

VectorQuantization?

指定搜索向量的自动向量量化类型。如果不设立此属性,则搜索不使用自动量化。

HnswMaxEdges

int?

设置节点在“可导航的分层小世界”图表中可以拥有的最大边数。

HnswNumEdgeCandidates

int?

设置要评估的最大节点数,以找到连接到新节点的最近邻。

IncludedStoredFields

IReadOnlyList<FieldDefinition<TDocument>>

指定要在索引中存储的字段。当您在查询选项中将 ReturnStoredSource设立为 true 时, MongoDB Vector Search 仅返回这些字段。您不能同时设立IncludedStoredFields 和 ExcludedStoredFields。

ExcludedStoredFields

IReadOnlyList<FieldDefinition<TDocument>>

指定要排除在索引中存储之外的字段。MongoDB Vector Search 存储所有其他字段。当您在查询选项中将 ReturnStoredSource设立为 true 时, MongoDB Vector Search 会返回这些存储的字段。您不能同时设立IncludedStoredFields 和 ExcludedStoredFields。

以下示例创建一个 CreateVectorSearchIndexModel实例,为名为 vs_idx 的索引提供规范。该代码将嵌入路径指定为 PlotEmbedding,这是一个类属性,对应于MongoDB中的 plot_embedding字段。它还为 1536 维度建立索引,并使用 Euclidean 向量相似度函数。

var model = new CreateVectorSearchIndexModel<Movie> (
model => model.PlotEmbedding,
"vs_idx",
VectorSimilarity.Euclidean,
1536);

要了解有关定义 MongoDB 向量搜索 搜索索引的更多信息,请参阅 Atlas 文档中的 如何为向量搜索字段编制索引。

您可以使用 CreateAutoEmbeddingVectorSearchIndexModel索引模型创建MongoDB Vector Search索引,该索引会自动为文本字段生成向量嵌入。

CreateAutoEmbeddingVectorSearchIndexModel 具有以下属性:

属性
类型
说明

AutoEmbeddingModelName

string

指定用于生成向量嵌入的嵌入模型的名称。有关支持的模型列表,请参阅 VoyageAI 文档中的文本嵌入。

Modality

VectorEmbeddingModality

指定要嵌入的数据类型。目前,唯一支持的模态是 VectorEmbeddingModality.Text。

Similarity

VectorSimilarity?

设置向量相似度函数以搜索前 K 个最近邻。如果您不设立此属性,则默认取决于 Quantization:当 Quantization 为 None 或 Scalar 时,默认值为 DotProduct;当 Quantization 为 Binary 或 BinaryNoRescore 时,默认值为 Euclidean。

Dimensions

int

指定嵌入模型的输出维度数。如果不设立此属性,驱动程序将使用嵌入模型的默认维度。

Quantization

VectorQuantization?

指定搜索向量的自动向量量化类型。如果不设立此属性,则搜索不使用自动量化。

HnswMaxEdges

int?

设置节点在“可导航的分层小世界”图表中可以拥有的最大边数。默认值为 16。

HnswNumEdgeCandidates

int?

设置在查找连接到新节点的最近邻时要评估的最大节点数。默认值为 100。

以下示例创建一个 CreateAutoEmbeddingVectorSearchIndexModel实例,为名为 auto_embedded_index 的索引提供规范。此索引使用 "voyage-4" 嵌入模型自动生成 plot字段的向量嵌入,还包括 runtime 和 year 字段的可选筛选器:

var model = new CreateAutoEmbeddingVectorSearchIndexModel<EmbeddedMovie>(
m => m.Plot,
"auto_embedding_index",
"voyage-4",
m => m.Runtime, m => m.Year // Optional filter fields
);

您可以通过在 IMongoCollection实例上调用 SearchIndexes.CreateOne() 方法来在集合上创建MongoDB Search 或MongoDB Vector Search索引。此方法接受在 CreateSearchIndexModel 或 CreateVectorSearchIndexModel实例中指定的索引模型作为参数。

以下示例在 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);

您可以通过在 IMongoCollection实例上调用 SearchIndexes.CreateMany() 方法来创建多个MongoDB Search 和MongoDB Vector Search 索引。此方法接受 CreateSearchIndexModel 或 CreateVectorSearchIndexModel 实例的 IEnumerable 作为参数。

此示例将执行以下动作:

  1. 创建一个 CreateSearchIndexModel实例,该实例指定名为 as_idx的 MongoDB搜索索引

  2. 创建一个 CreateVectorSearchIndexModel实例,该实例指定名为 vs_idx 的MongoDB Vector Search索引

  3. 将 CreateSearchIndexModel 和 CreateVectorSearchIndexModel 实例的 List 传递给 SearchIndexes.CreateMany() 方法

  4. 在 embedded_movies 集合上创建 MongoDB 搜索搜索索引 和 MongoDB Vector 搜索搜索索引

var searchModel = new CreateSearchIndexModel(
"as_idx",
SearchIndexType.Search,
new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", true },
} }
}
);
var vectorModel = new CreateVectorSearchIndexModel<Movie>(
m => m.PlotEmbedding,
"vs_idx",
VectorSimilarity.Euclidean,
1536);
var models = new List<CreateSearchIndexModel> { searchModel, vectorModel };
var indexes = movieCollection.SearchIndexes.CreateMany(models);
Console.WriteLine("Created Search indexes:\n{0} {1}", indexes.ToArray());

您可以通过调用集合上的 SearchIndexes.List() 方法来访问有关集合现有的 MongoDB 搜索和 MongoDB 向量搜索搜索索引的信息。

The following example accesses information about the MongoDB Search and MongoDB Vector Search indexes created in the Create Multiple Search Indexes section of this page. The code calls the SearchIndexes.List() method and prints a list of the MongoDB Search and MongoDB Vector Search indexes on the collection:

var indexesList = movieCollection.SearchIndexes.List().ToList();
foreach (var i in indexesList)
{
Console.WriteLine(i);
}

您可以通过在 IMongoCollection 实例上调用 SearchIndexes.Update() 方法来更新 MongoDB 搜索 或 MongoDB 向量搜索 索引。此方法接受以下参数:

  • 要更新的索引名称

  • 修改索引定义文档

The following example updates the Vector Search index named vs_index created in the Create Multiple Search Indexes section of this page. The code creates a new index definition document that instructs the index to use "dotProduct" as the vector similarity function. Then, the code calls the SearchIndexes.Update() method to update the index:

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 向量搜索索引。此方法接受要删除的索引名称作为参数。

The following example deletes the MongoDB Search index named example_index created in the Create a Search Index section of this page. The code passes the index name to the SearchIndexes.DropOne() method to delete the index:

movieCollection.SearchIndexes.DropOne("example_index");

要学习;了解可以使用.NET/ C#驱动程序创建的其他索引,请参阅创建和管理索引指南。

要学习;了解有关MongoDB Search 的更多信息,请参阅以下Atlas文档:

要学习;了解有关MongoDB Vector Search 的更多信息,请参阅以下Atlas文档:

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: