Docs Menu
Docs Home
/ /

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:

  • 検索インデックスモデルの作成

  • 検索インデックスを作成

  • 複数の検索インデックスの作成

  • 検索インデックスをリストする

  • 検索インデックスをアップデートする

  • 検索インデックスを削除する

注意

サンプル データ

このガイドの例では、Atlasサンプルデータセットの 1 つである sample_mflixデータベース内の embedded_moviesコレクションを使用します。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 という名前のインデックスの仕様を提供します。コードでは、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
);

次の例では、vs_idx という名前のインデックスの仕様を提供するための CreateSearchIndexModel インスタンスを作成します。コードは、埋め込みパスを 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 インスタンスの ListSearchIndexes.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ドキュメントを参照してください。

戻る

Indexes

項目一覧