Docs Menu
Docs Home
/ /

MongoDB 検索する と MongoDB ベクトル検索インデックス

このガイドでは、MongoDB Search およびMongoDB ベクトル検索インデックスを作成および学ぶことができます。これらのインデックスを使用すると、次の機能を使用できます。

  • MongoDB 検索する:高速で全文検索を実行

  • MongoDB ベクトル検索: ベクトル埋め込みに対してセマンティック(類似性)検索を実行

MongoDB 検索およびMongoDB ベクトル検索インデックスは、インデックスを作成するフィールドを指定し、これらのフィールドにインデックスを作成する方法を指定し、その他の任意の構成を設定します。

注意

MongoDB Search 検索インデックス マネジメントのメソッドは非同期で実行されます。ドライバー メソッドは、サーバー上で目的のアクションが完了する前に結果を返すことができます。

このガイドでは、次のアクションを実行してMongoDB 検索するおよびMongoDB ベクトル検索インデックスを管理する方法について説明します。

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

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

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

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

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

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

注意

サンプル データ

このガイドの例では、Atlasサンプルデータセットの 1 つである sample_mflixデータベース内の embedded_moviesコレクションを使用します。Atlasサンプルデータのインポート手順については、Atlas ドキュメントのサンプル データのロードを参照してください。

The following sections describe how to create index models for MongoDB Search and MongoDB Vector Search indexes.

To create a MongoDB Search index, you must construct a CreateSearchIndexModel instance that sets your index specifications.

The CreateSearchIndexModel class has the following properties:

プロパティ
タイプ
説明

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 検索するフィールドマッピングの詳細については、Atlas ドキュメントのフィールドマッピングの定義を参照してください。

To create a MongoDB Vector Search index, you must construct a CreateVectorSearchIndexModel instance that sets your index specifications.

The CreateVectorSearchIndexModel class inherits from the CreateSearchIndexModel class and has the following additional properties:

プロパティ
タイプ
説明

Field

FieldDefinition<TDocument>

Specifies the field that contains the vectors to index.

Similarity

VectorSimilarity

Sets the vector similarity function to use to search for the top K-nearest neighbors.

Dimensions

int

Specifies the number of dimensions that the search enforces at index-time and query-time.

FilterFields

IReadOnlyList<FieldDefinition<TDocument>>

Specifies the fields that the search uses as filters in the vector query.

Quantization

VectorQuantization?

Specifies the type of automatic vector quantization for the search vectors. If you don't set this property, the search uses no automatic quantization.

HnswMaxEdges

int?

Sets the maximum number of edges that a node can have in the Hierarchical Navigable Small Worlds graph.

HnswNumEdgeCandidates

int?

Sets the maximum number of nodes to evaluate to find the closest neighbors to connect to a new node.

The following example creates a CreateVectorSearchIndexModel instance to provide specifications for an index named vs_idx. The code specifies the embedding path as PlotEmbedding, a class property that corresponds to the plot_embedding field in MongoDB. It also indexes 1536 dimensions, and uses the Euclidean vector similarity function.

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

MongoDB ベクトル検索の検索インデックスの定義の詳細については、Atlas ドキュメントのベクトル検索のフィールドにインデックスを作成する方法を参照してください。

You can use a CreateAutoEmbeddingVectorSearchIndexModel index model to create a MongoDB Vector Search index that automatically generates vector embeddings for text fields.

The CreateAutoEmbeddingVectorSearchIndexModel has the following properties, in addition to the properties inherited from CreateVectorSearchIndexModelBase<TDocument>:

プロパティ
タイプ
説明

AutoEmbeddingModelName

string

Specifies the name of the embedding model to use for generating vector embeddings. For a list of supported models, see Text Embeddings in the VoyageAI documentation.

Modality

VectorEmbeddingModality

Specifies the type of data to embed. Currently, the only supported modality is VectorEmbeddingModality.Text.

The following example creates a CreateAutoEmbeddingVectorSearchIndexModel instance that provides specifications for an index named auto_embedded_index. This index uses the "voyage-4" embedding model to automatically generate vector embeddings for the plot field, and also includes optional filters for the runtime and year fields:

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

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 or CreateVectorSearchIndexModel 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"

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 or CreateVectorSearchIndexModel instances as a parameter.

この例では、次のアクションを実行します。

  1. as_idx という名前のMongoDB 検索インデックスを指定する CreateSearchIndexModelインスタンスを作成します

  2. vs_idx という名前のMongoDB ベクトル検索インデックスを指定する CreateVectorSearchIndexModelインスタンスを作成します

  3. Passes a List of the CreateSearchIndexModel and CreateVectorSearchIndexModel instances to the SearchIndexes.CreateMany() method

  4. embedded_moviesコレクションにMongoDB Search 検索インデックスとMongoDB ベクトル検索インデックスを作成します

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());
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": [...] }

MongoDB Search または MongoDB ベクトル検索インデックスを更新するには、IMongoCollectionインスタンスで SearchIndexes.Update() メソッドを呼び出します。このメソッドは次のパラメーターを受け入れます:

  • 更新するインデックスの名前

  • 変更されたインデックス定義ドキュメント

以下の例は、このページの「複数のベクトル検索インデックスの作成」セクションで作成された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ドキュメントを参照してください。

戻る

Indexes