Overview
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 には次のプロパティがあります。
プロパティ | タイプ | 説明 |
|---|---|---|
|
| Specifies the index definition. If you omit this setting, the driver creates a MongoDB Search index with dynamic mappings. |
|
| インデックス名を設定します。この設定を省略すると、ドライバーは名前を |
|
| 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.
例
この例では、次のアクションを実行します。
Creates a
CreateSearchIndexModelinstance that specifies a MongoDB Search index namedas_idxCreates a
CreateSearchIndexModelinstance that specifies a MongoDB Vector Search index namedvs_idx両方の
CreateSearchIndexModelインスタンスのListをSearchIndexes.CreateMany()メソッドに渡しますCreates the MongoDB Search and MongoDB Vector Search indexes on the
embedded_moviescollection
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 ドキュメント
このガイドで言及されているメソッドとタイプの詳細については、次のAPIドキュメントを参照してください。