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

ベクトル検索近似最近傍探索および厳密最近傍探索クエリの実行

A MongoDB Vector Search query takes the form of an aggregation pipeline that uses $vectorSearch as the first stage. This page explains the syntax, options, and behavior of the $vectorSearch stage.

$vectorSearch

$vectorSearchステージは、次のフィールドを持つドキュメントを取得します。

When you define a $vectorSearch stage, you can use the exact field to specify whether to run an ANN or ENN search.

近似近傍(近似最近傍探索)検索

近似最近傍探索(ANN)では、MongoDB ベクトル検索は、多次元空間内での近接性と考慮する近傍の数に基づいて、クエリ内のベクトル埋め込みに最も近いデータ内のベクトル埋め込みを検索します。これは Hierarchical Navigable Small Worldsアルゴリズムを使用し、すべてのベクトルをスキャンすることなく、クエリ内のベクトル埋め込みに最も類似するベクトル埋め込みを見つけます。したがって、近似最近傍探索検索は、重要なフィルターなしで大規模なデータセットをクエリするのに最適です。

注意

ANN 検索の最適な呼び出しは、通常、enn 検索との結果の重複が約 90-95% あたりであると考えられますが、レイテンシは大幅に低くなります 。これにより、精度とパフォーマンスの適切なバランスが実現できます。 MongoDB ベクトル検索でこれを実現するには、クエリ時に numCandidates パラメータを調整します。

近似最近傍探索を検索するには、numCandidatesフィールドを指定する必要があります。このフィールドは、MongoDB ベクトル検索が検索する際に考慮する最近傍の数を決定します。

When you perform a vector search using the Hierarchical Navigable Small Worlds index structure, MongoDB Vector Search accumulates results by populating a priority queue. The numCandidates parameter controls the size of this queue, which determines how long to search before returning the top limit results. A larger queue (higher numCandidates) allows the search to explore more of the Hierarchical Navigable Small Worlds graph, potentially finding better matches at the cost of increased query latency.

We recommend that you specify a numCandidates number at least 20 times higher than the number of documents to return (limit) to increase accuracy and reduce discrepancies between your ENN and ANN query results. For example, if you set limit to return 5 results, consider setting numCandidates to 100 as a starting point. To learn more, see How to Measure the Accuracy of Your Query Results.

This overrequest pattern is the recommended way to trade off latency and recall in your ANN searches. However, we recommend tuning the numCandidates parameter based on your specific dataset size and query requirements. To ensure that you get accurate results, consider the following variables:

完全厳密最近傍探索

厳密最近傍探索の場合、MongoDB ベクトル検索は、すべての埋め込み間の距離を計算してすべてのインデックスが付けられたベクトル埋め込みを網羅的に検索し、クエリ内のベクトル埋め込みに対して厳密最近傍探索を見つけます。これは計算負荷が高く、 クエリレイテンシに悪影響を影響可能性があります。したがって、次のユースケースには 厳密最近傍探索 が推奨されます。

$vectorSearch は、それが表示されるすべてのパイプラインの最初のステージである必要があります。

$vectorSearch can't be used in view definition and the following pipeline stages:

[1] You can pass the results of $vectorSearch to this stage.

これらのMongoDB ベクトル検索フィールドタイプの詳細については、「 ベクトル検索のフィールドにインデックスを作成する方法 」を参照してください。

MongoDB ベクトル検索 は、返されるすべてのドキュメントに、0 から 1 までの固定範囲のスコアを割り当てます(0 は類似性が低く、1 は類似性が高いことを示します)。

注意

Pre-filtering your data doesn't affect the score that MongoDB Vector Search returns using vectorSearchScore for $vectorSearch queries.

MongoDB ベクトル検索はデータのフィルターをサポートします。次の作業が可能です。

Atlas Vector Search 事前フィルターおよび事後フィルター

MongoDB ベクトル検索は、データのセグメントに対してプレフィルター操作と書き込みフィルター操作を独立して実行します。各セグメントの HNSW グラフは、そのセグメント内のベクトルのみに基づいています。MongoDB ベクトル検索は、フィルター条件を満たさないドキュメントを除外するために、各セグメントのドキュメントにフィルターを適用します。プレフィルターにより、MongoDB ベクトル検索が HNSW グラフをトラバースする前にドキュメントが除外され、書き込みフィルターにより、ドキュメント内の関連のないドキュメントまたはフィールドがベクトル検索結果から除外されます。

データを事前フィルターすることで、セマンティック検索の範囲を絞り込み、比較対象として関連するベクトルのみが考慮されるようにできます。filter オプションを使用してデータを事前フィルターすると、MongoDBベクトル検索はデータのサブセットのみでセマンティック検索を実行し、検索結果の精度が向上します。

プレフィルターは制限が厳しすぎる可能性があります。プレフィルターはプレフィルターと正確に一致しないデータを除外する可能性がありますが、そのデータはベクトル検索中に考慮するクエリと意味的に類似しています。これを軽減するには、広範なフィルター条件を設定して、クエリが次のことを実行するようにすることをお勧めします。

  • 可能な限り多くの関連する結果が含まれます。

  • 結果から関係のないデータを除外します。

  • 結果におけるリクエストされた数のドキュメントを返します。

重要

フィルターされたクエリは通常、同等のフィルターされていないクエリよりも遅くなります。

If your index size isn't optimal for pre-filtering or if you set broad pre-filtering criteria, you can post-filter your vector search results to return only relevant data. To filter the results of your vector search, you can use any supported aggregation pipeline stage, such as the $match stage, after the $vectorSearch stage. To learn more, see Additional Performance Recommendations.

To select the fields to return in the results, use the $project stage unless you need all the fields in the results. We recommend excluding the vector field in the $project stage to improve query performance.

For example, you can use the $project stage to include the vectorSearchScore and then use the $match stage to return only documents above a certain score threshold.

  • MongoDB Vector Search supports the short form of $eq. In the short form, you don't need to specify $eq in the query.

    $eq の付いた次のフィルターを例に挙げます。

    "filter": { "_id": { "$eq": ObjectId("5a9427648b0beebeb69537a5") }

    これは、$eq の短縮形を使用する次のフィルターと同等です。

    "filter": { "_id": ObjectId("5a9427648b0beebeb69537a5") }
  • You can use the $and MQL operator to specify an array of filters in a single query.

    たとえば、genres フィールドが Action に等しく、かつ year フィールドの値が 19992000、または 2001 に等しいドキュメントの次のプレフィルターを考えてみましょう。

    "filter": {
    "$and": [
    { "genres": "Action" },
    { "year": { "$in": [ 1999, 2000, 2001 ] } }
    ]
    }
  • For advanced filtering capabilities such as fuzzy search, phrase matching, location filtering, and other analyzed text, use the vectorSearch operator in a $search stage.

これらの例を実行する前に、次のアクションを実行する必要があります。

  • Add the sample dataset used in the query to your cluster.

  • コレクションの MongoDB ベクトル検索インデックスを作成します。手順については、MongoDB ベクトル検索インデックスの作成手順を参照し、希望する言語のサンプル クエリに合わせてインデックスを構成します。

次のチュートリアルでは、$vectorSearch ステージのその他のユースケースを示します。