AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

ベクトル検索のためのベクトル埋め込みのインデックス作成方法

knnVector 型を使用してベクトル埋め込みをインデックスできます。ベクトル フィールドは、次の型の番号の配列として表せます。

  • BSON int32knnBeta演算子を使用してクエリを実行するための、 int64 、またはdouble データ型。

  • BSON double data type for querying using the $vectorSearch stage.

You can use the knnBeta operator, which is now deprecated, and the $vectorSearch stage in your aggregation pipeline to query fields indexed as knnVector.

注意

Atlas UIのMongoDB Search Visual Editor を使用して、knnVector 型のフィールドを設定することはできません。代わりに、 MongoDB Search JSON Editor を使用して、knnVector 型のフィールドを構成します。

You can also use MongoDB Vector Search with local Atlas deployments that you create with the Atlas CLI. To learn more, see Create a Local Atlas Deployment.

ドキュメントの配列内のフィールド、またはオブジェクトの配列内のフィールド( MongoDB Search embeddedDocuments 型)を knnVector タイプとしてインデックスはできません。

The following is the JSON syntax for the knnVector type. Replace the default index definition with the following. To learn more about the fields, see Field Properties.

1{
2 "mappings": {
3 "dynamic": true|false,
4 "fields": {
5 "<field-name>": {
6 "type": "knnVector",
7 "dimensions": <number-of-dimensions>,
8 "similarity": "euclidean | cosine | dotProduct"
9 }
10 }
11 }
12}

knnVector 型には、次のオプションがあります。

オプション
タイプ
必要性
目的

type

string

必須

このフィールド型を識別する、人間が判読できるラベル。値はknnVectorでなければなりません。

dimensions

整数

必須

MongoDB Search がインデックス時およびクエリ時に強制されるベクトル次元の数。この値は 8192 より大きくすることはできません。

similarity

string

必須

上位 K 近傍の検索に使用するベクトル類似度関数。値は次のいずれかになります。

  • euclidean - measures the distance between ends of vectors. This allows you to measure similarity based on varying dimensions. To learn more, see Euclidean.

  • cosine - measures similarity based on the angle between vectors. This allows you to measure similarity that isn't scaled by magnitude. You can't use zero magnitude vectors with cosine. To measure cosine similarity, we recommend that you normalize your vectors and use dotProduct instead. To learn more, see Cosine.

  • dotProduct - measures similarity like cosine, but takes into account the magnitude of the vector. This allows you to efficiently measure similarity based on both angle and magnitude. To learn more, see Dot Product.

    大きさを正規化すると、類似性の測定において cosine と dotProduct はほぼ同じになります。

sample_mflix.embedded_movies コレクションの次のインデックス定義は、コレクション内のすべての動的にインデックス可能なフィールドを動的にインデックスされ、plot_embedding フィールドを knnVector 型として静的にインデックスされます。plot_embedding フィールドには、OpenAI の text-embedding-ada-002 埋め込みモデルを使用して作成された埋め込みが含まれます。インデックス定義では、1536 ベクトル次元を指定し、 euclidean を使用して類似性を測定します。

1{
2 "mappings": {
3 "dynamic": true,
4 "fields": {
5 "plot_embedding": {
6 "type": "knnVector",
7 "dimensions": 1536,
8 "similarity": "euclidean"
9 }
10 }
11 }
12}

If you load the sample data on your cluster and create the preceding MongoDB Search index for this collection, you can run $vectorSearch queries against this collection. To learn more about the sample queries that you can run, see $vectorSearch Examples.

MongoDB 検索インデックスをベクトル型に移行するには、ベクトルフィールドのインデックス定義内の knnVector 型を vector 型に置き換えます。

例

To use the vector type for the plot_embedding field in the sample index definition for the sample_mflix.embedded_movies collection, replace the index definition with the following:

1{
2 "mappings": {
3 "dynamic": true,
4 "fields": {
5 "plot_embedding": {
6 "type": "vector",
7 "dimensions": 1536,
8 "similarity": "euclidean"
9 }
10 }
11 }
12}

vector型フィールドに対してクエリを実行するには、 演算子の代わりに vectorSearch(MongoDB Search 演算子)を使用する必要があります。knnBeta

注意

既存のknnBeta 演算子クエリは、vector 型インデックスに対して引き続き実行できます。インデックスを移行した後、すぐに knnBeta 演算子クエリを vectorSearch(MongoDB 検索する 演算子) に移行することをお勧めします。