Overview
このガイドでは、MongoDB Search およびMongoDB ベクトル検索インデックスを作成および学ぶことができます。これらのインデックスを使用すると、次の機能を使用できます。
MongoDB 検索する:高速で全文検索を実行
MongoDB ベクトル検索: ベクトル埋め込みに対してセマンティック(類似性)検索を実行
MongoDB 検索およびMongoDB ベクトル検索インデックスは、インデックスを作成するフィールドを指定し、これらのフィールドにインデックスを作成する方法を指定し、その他の任意の構成を設定します。
注意
MongoDB Search 検索インデックス マネジメントのメソッドは非同期で実行されます。ドライバー メソッドは、サーバー上で目的のアクションが完了する前に結果を返すことができます。
このガイドでは、次のアクションを実行してMongoDB 検索するおよびMongoDB ベクトル検索インデックスを管理する方法について説明します。
注意
サンプル データ
このガイドの例では、Atlasサンプルデータセットの 1 つである sample_mflixデータベース内の embedded_moviesコレクションを使用します。Atlasサンプルデータのインポート手順については、Atlas ドキュメントのサンプル データのロードを参照してください。
検索インデックスモデルの作成
MongoDB 検索する または MongoDB ベクトル検索インデックスを作成するには、まずインデックス仕様を設定する CreateSearchIndexModelインスタンスをビルドする必要があります。
CreateSearchIndexModel には次のプロパティがあります。
プロパティ | タイプ | 説明 |
|---|---|---|
|
| インデックスの定義を指定します。この設定を省略すると、ドライバーは動的マッピングを含むMongoDB 検索インデックスを作成します。 |
|
| インデックス名を設定します。この設定を省略すると、ドライバーは名前を |
|
| インデックスのタイプを設定します。この設定を省略すると、ドライバーはデフォルトでMongoDB検索インデックスを作成します。 |
MongoDB 検索するフィールドマッピングの詳細については、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 );
次の例では、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.
例
次の例では、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 ベクトル検索の検索インデックスを作成できます。このメソッドは、CreateSearchIndexModel インスタンスの IEnumerable をパラメータとして受け入れます。
例
この例では、次のアクションを実行します。
as_idxという名前のMongoDB 検索インデックスを指定するCreateSearchIndexModelインスタンスを作成しますvs_idxという名前のMongoDB ベクトル検索インデックスを指定するCreateSearchIndexModelインスタンスを作成します両方の
CreateSearchIndexModelインスタンスのListをSearchIndexes.CreateMany()メソッドに渡しますembedded_moviesコレクションにMongoDB Search 検索インデックスとMongoDB ベクトル検索インデックスを作成します
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 Search 検索インデックスおよびMongoDB ベクトル検索インデックスに関する情報にアクセスできます。
例
次の例では、このページの複数の検索インデックスの作成セクションで作成されたMongoDB SearchおよびMongoDB ベクトル検索インデックスに関する情報にアクセスします。このコードでは 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);
検索インデックスを削除する
MongoDB Search または MongoDB ベクトル検索インデックスを削除するには、IMongoCollection インスタンスで SearchIndexes.DropOne() メソッドを呼び出します。このメソッドは、削除するインデックスの名前をパラメータとして受け入れます。
例
次の例では、このページの検索インデックスの作成セクションで作成された example_index という名前のMongoDB検索インデックスを削除します。このコードはインデックス名を SearchIndexes.DropOne() メソッドに渡して、インデックスを削除します。
movieCollection.SearchIndexes.DropOne("example_index");
詳細情報
.NET/ C#ドライバーを使用して作成できるその他のインデックスの詳細については、インデックスの作成と管理ガイドを参照してください。
MongoDB Search の詳細については、次の Atlas ドキュメントを参照してください。
MongoDB ベクトル検索の詳細については、次の Atlas ドキュメントを参照してください。
API ドキュメント
このガイドで言及されているメソッドとタイプの詳細については、次のAPIドキュメントを参照してください。