Docs 菜单
Docs 主页
/ /

MongoDB Search and MongoDB Vector Search Indexes

In this guide, you can learn how to create and manage MongoDB Search and MongoDB Vector Search indexes. These indexes allow you to use the following features:

  • MongoDB Search: Perform fast, full-text searches

  • MongoDB Vector Search: Perform semantic (similarity) searches on vector embeddings

MongoDB Search and MongoDB Vector Search indexes specify which fields to index, specify how these fields are indexed, and set other optional configurations.

注意

MongoDB Search index-management methods run asynchronously. The driver methods can return a result before the desired action completes on the server.

This guide explains how to perform the following actions to manage your MongoDB Search and MongoDB Vector Search indexes:

  • 创建搜索索引模型

  • 创建搜索索引

  • 创建多个搜索索引

  • 搜索索引列表

  • 更新搜索索引

  • 删除搜索索引

注意

样本数据

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

To create a MongoDB Search or MongoDB Vector Search index, you must first build a CreateSearchIndexModel instance that sets your index specifications.

CreateSearchIndexModel 具有以下属性:

属性
类型
说明

definition

BsonDocument

Specifies the index definition. If you omit this setting, the driver creates a MongoDB Search index with dynamic mappings.

name

string

设置索引名称。如果省略此设置,驾驶员会将名称设置为 default

type

SearchIndexType

Sets the index type. If you omit this setting, the driver creates a MongoDB Search index by default.

To learn more about MongoDB Search field mappings, see Define Field Mappings in the Atlas documentation.

To learn more about defining MongoDB Vector Search indexes, see How to Index Fields for Vector Search in the Atlas documentation.

以下示例创建一个 CreateSearchIndexModel实例,为名为 search_idx 的索引提供规范。该代码指定了 titlereleased 字段的静态映射:

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.

The following example creates a MongoDB Search index on the embedded_movies collection. The code creates a CreateSearchIndexModel that sets the index name and enables dynamic mapping. Then, the code passes the CreateSearchIndexModel instance to the SearchIndexes.CreateOne() method to create the MongoDB Search index:

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"

You can create multiple MongoDB Search and MongoDB Vector Search indexes by calling the SearchIndexes.CreateMany() method on an IMongoCollection instance. This method accepts an IEnumerable of CreateSearchIndexModel instances as a parameter.

此示例将执行以下动作:

  1. Creates a CreateSearchIndexModel instance that specifies a MongoDB Search index named as_idx

  2. Creates a CreateSearchIndexModel instance that specifies a MongoDB Vector Search index named vs_idx

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

  4. Creates the MongoDB Search and MongoDB Vector Search indexes on the embedded_movies collection

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

You can access information about a collection's existing MongoDB Search and MongoDB Vector Search indexes by calling the SearchIndexes.List() method on the collection.

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);
}
{ "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);

You can delete a MongoDB Search or MongoDB Vector Search index by calling the SearchIndexes.DropOne() method on an IMongoCollection instance. This method accepts the name of the index to delete as a parameter.

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#驱动程序创建的其他索引,请参阅创建和管理索引指南。

To learn more about MongoDB Search, see the following Atlas documentation:

To learn more about MongoDB Vector Search, see the following Atlas documentation:

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

后退

索引

在此页面上