定義
$vectorSearchステージでは、各ストリーミングドキュメントがMongoDB ベクトル検索クエリの結果で豊富になります。入力ドキュメントごとに、ステージはコレクションに対して $vectorSearch クエリを実行し、指定されたフィールドに結果をアタッチします。
配置
$vectorSearch は、パイプラインの中間ステージ である必要があります。パイプラインの または Sink$source として使用しないでください。
構文
$vectorSearchパイプライン ステージには次のプロトタイプ形式があります。
{ "$vectorSearch": { "from": { "connectionName": "<registered-atlas-connection>", "db": "<database>", "coll": "<collection>" }, "as": "<output-field>", "queryVector": [<number>, ...] | <expression>, "query": { "text": "<string>" | <expression> }, "model": "<embedding-model>", "index": "<vector-index-name>", "path": "<field-containing-vectors>", "numCandidates": <int>, "limit": <int>, "filter": { ... }, "exact": <bool>, "searchNodePreference": { "key": "<preference>" }, "pipeline": [ { $<aggregation-stage>: { ... } }, ... ], "let": { "<variable>": <expression>, . . . } } }
$vectorSearchステージは、次のフィールドを持つドキュメントを取得します。
フィールド | タイプ | 必要性 | 説明 |
|---|---|---|---|
| ドキュメント | 必須 | 各入力ドキュメントに対して |
| string | 必須 | 接続レジストリ内の Atlas 接続の名前。 Atlas Stream Processing は、解析時に他の接続タイプを拒否します。 |
| literal string | 必須 | ターゲットデータベースの名前。 |
| literal string | 必須 | MongoDB ベクトル検索インデックスを含むターゲットコレクションの名前。 |
| string | 必須 | 検索結果配列のストリーミングドキュメントの出力フィールド。 MongoDB ベクトル検索 が一致するドキュメントを返さない場合、Atlas Stream Processing はこのフィールドを空の配列に設定します。 警告: このフィールドが空であるか、ストリームメタデータのフィールド名と競合する場合、Atlas Stream Processing は起動時に |
| 配列 |フィールドパス式 | 条件付き | |
| ドキュメント | 条件付き | |
| string |フィールドパス式 | 条件付き | 自動埋め込みに使用するクエリ テキスト。 |
| string | 任意 | 埋め込みモデルMongoDB ベクトル検索 はを使用して
|
| string | 必須 | クエリするMongoDB ベクトル検索インデックスの名前。 |
| string | 必須 | 検索対象のベクトル埋め込みを含むターゲットコレクション内のフィールド。 |
| 整数 | 条件付き | 検索中にMongoDB ベクトル検索 が考慮する候補の数。 |
| 整数 | 必須 | 返される結果の最大数。 警告: |
| ドキュメント | 任意 | ベクトル検索の前に適用するプレフィルター式。サポートされている演算子については、「 フィルター 」を参照してください。 警告: |
| ブール値 | 任意 | Ann 検索または ENN 検索のどちらを実行するかを指定するフラグ。 値は次のいずれかになります。
デフォルトは |
| ドキュメント | 任意 | Atlas Stream Processing がクエリを特定の 検索ノードにルーティングするために使用する |
| 配列 | 任意 | Atlas Stream Processing が検索結果を入力ドキュメントに添付する前に、検索結果に対して実行する追加の集計ステージ 。 |
| ドキュメント | 任意 |
動作
クエリ モード
$vectorSearch queryVectorは、2 query.textつの相互に排他的なクエリモードをサポートします。ベクトル埋め込みでは 、自動埋め込みでは です。モードのどちらでも、 リテラル値 またはフィールドパス式を指定できるため、単一のストリーム プロセッサが 入力ドキュメントごとに異なるクエリを発行できます。
Error Handling
ターゲットMongoDB ベクトル検索インデックスが存在しない場合、ストリーム プロセッサは $vectorSearch の最初の実行時にエラー状態になります。 Atlas Stream Processing は MongoServerError エラーを報告し、ドキュメントの処理を停止します。
queryVectorquery.textまたは が実行時に間違ったタイプと評価された場合、Atlas Stream Processing は、競合ドキュメントのみをデッドレターキュー(DLQ)にルーティングするか、そうでない場合は削除します。ストリーム プロセッサは、後のドキュメントの処理を続行します。 DLQドキュメントスキーマについては、「 デッド文字キュー 」を参照してください。
処理結果
Atlas Stream Processing が検索結果を入力ドキュメントにアタッチする前に、pipeline を使用して検索結果をさらに処理します。 Atlas Stream Processing は pipeline をMongoDB ベクトル検索 の冗長性に転送します。 pipeline 内の入力ドキュメントの値を参照には、その値を let に渡し、$$<variable> として参照。
例
次の例では、映画と映画館に関するデータが含まれる sample_mflix データセット をクエリします。これらを実行するには、サンプルデータを Atlas クラスターにロードし、 コレクションにMongoDBembedded_movies ベクトル検索インデックスを作成します。
次の例では、受信検索イベントを映画の推奨事項で豊富にしています。各入力ドキュメントには topicフィールドがあります。このパイプラインはquery.text を使用し、 MongoDB ベクトル検索 はvoyage-4-lite モデルを使用して各ドキュメントの topic 値の埋め込みを生成します。この集計には 3 つのステージがあります。
ステージは
$sourceAtlas データベースとの接続を確立し、具体的には データベース内のsearchesコレクションを対象とします。appfullDocumentOnlyオプションでは、ソースコレクションで変更前と変更後のイメージを有効にする必要があります。$vectorSearchステージでは、各ドキュメントのtopicフィールドのテキストを使用して、sample_mflix.embedded_moviesコレクションのplot_vector_indexインデックスをクエリし、最大 5 つの結果をrecommendationsフィールドに添付します。pipelineフィールドは、各結果をtitleフィールドとplotフィールドに制限します。$mergeステージは、エンタープライズなドキュメントをenriched_searchesデータベース内のappコレクションに書き込みます。
{ "$source": { "connectionName": "srcCluster", "db": "app", "coll": "searches", "config": { "fullDocument": "required", "fullDocumentOnly": true } }, "$vectorSearch": { "from": { "connectionName": "atlasCluster", "db": "sample_mflix", "coll": "embedded_movies" }, "as": "recommendations", "query": { "text": "$topic" }, "model": "voyage-4-lite", "index": "plot_vector_index", "path": "plot", "numCandidates": 50, "limit": 5, "pipeline": [ { "$project": { "_id": 0, "title": 1, "plot": 1 } } ] }, "$merge": { "into": { "connectionName": "atlasCluster", "db": "app", "coll": "enriched_searches" } } }
出力コレクションに書き込まれるエンタープライズ ドキュメントは、次のようになります。
{ _id: ObjectId('6a4beebd75f00b6ddf98a8c0'), topic: 'gangster crime drama', recommendations: [ { title: 'Scarface', plot: 'An ambitious and near insanely violent gangster climbs the ladder of success in the mob, but his weaknesses prove to be his downfall.' }, { title: 'That Demon Within', plot: 'A dutiful cop, guilt-ridden over saving the life of a gang leader, becomes obsessed with bringing down the crime syndicate of the man he saved.' }, { title: 'Billa 2', plot: 'An ordinary man from the slums enters an underworld gang and becomes the most feared underworld don.' }, { title: 'A Better Tomorrow', plot: 'A reforming ex-gangster tries to reconcile with his estranged policeman brother, but the ties to his former gang are difficult to break.' }, { title: 'Singham', plot: 'A humiliated gangster uses his influence and goon power to terrorize a newly transferred police officer.' } ] }
次の例では、 RAGパイプラインの高精度の映画プロット パスを検索します。各受信イベントには事前計算されたqueryEmbedding フィールドがすでに含まれているため、パイプラインは を使用します。queryVector $vectorSearchステージでは、再現率を最大化するために幅広い候補パスのセットが検索されます。 の$rerank pipelineステージでは、それらの候補の順序が変更され、最も関連性の高いパスのみが下流に続くようになります。この集計には 4 つのステージがあります。
ステージは
$sourceAtlas データベースとの接続を確立し、具体的には データベース内のeventsコレクションを対象とします。llm_requestsfullDocumentOnlyオプションでは、ソースコレクションで変更前と変更後のイメージを有効にする必要があります。$vectorSearchステージでは、各ドキュメントのqueryEmbeddingフィールドをqueryVectorとして使用して、sample_mflix.embedded_moviesコレクションのplot_embedding_indexインデックスをクエリします。再現率を最大化するにはnumCandidatesを高く設定し、50 がpipelineと一致するまで渡します。pipeline内:$rerankrerank-2.5-liteは、 モデルを使用して、クエリテキストに関連することで候補を並べ替えます。$limitは、最も関連性の高い 8 つのフレーズを保持します。$projectは、プロンプトを構築するために必要なフィールドのみを返します。
query.text値はletで定義されたsearchText変数を参照します。これは各ドキュメントのqueryTextフィールドに解決されます。 Atlas Stream Processing は結果をcontextフィールドに保存します。$unsetqueryEmbeddingqueryTextステージでは、下流で不要になったため、 フィールドと フィールドが削除されます。$it ステージは、エンタープライズなドキュメントを
qa_enrichedトピックに送信します。
重要
を使用するパイプラインを実行する前に、Atlasプロジェクト管理者がネイティブ再ランク付けを有効にしていることを確認してください。$rerank
{ "$source": { "connectionName": "appCluster", "db": "llm_requests", "coll": "events", "config": { "fullDocument": "required", "fullDocumentOnly": true } }, "$vectorSearch": { "from": { "connectionName": "atlasCluster", "db": "sample_mflix", "coll": "embedded_movies" }, "as": "context", "queryVector": "$queryEmbedding", "index": "plot_embedding_index", "path": "plot_embedding", "numCandidates": 200, "limit": 50, "let": { "searchText": "$queryText" }, "pipeline": [ { "$rerank": { "query": { "text": "$$searchText" }, "path": "plot", "model": "rerank-2.5-lite", "numDocsToRerank": 50 } }, { "$limit": 8 }, { "$project": { "_id": 0, "title": 1, "plot": 1 } } ] }, "$unset": ["queryEmbedding", "queryText"], "$emit": { "connectionName": "appKafka", "topic": "qa_enriched" } }
トピックに出力されるドキュメントは、次のようになります。
{ _id: ObjectId('6a4c0cbc37d690084f3115da'), context: [ { title: 'A Better Tomorrow', plot: 'A reforming ex-gangster tries to reconcile with his estranged policeman brother, but the ties to his former gang are difficult to break.' }, { title: 'Yuma', plot: 'A story about the rise and fall of Zyga - a Polish kid in his early twenties.' }, { title: 'Ghost Dog: The Way of the Samurai', plot: 'An African American mafia hit man who models himself after the samurai of old finds himself targeted for death by the mob.' } ... ] }