定义
$vectorSearch 阶段使用 MongoDB 向量搜索查询的结果丰富每个流媒体文档。对于每个输入文档,该阶段都会针对集合运行 $vectorSearch 查询,并将结果附加到您指定的字段。
展示位置
$vectorSearch 必须是管道的中间阶段。请勿将其用作管道的$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 阶段采用包含以下字段的文档:
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 文档 | 必需 | 指定 |
| 字符串 | 必需 | 连接注册表中 Atlas 连接的名称。Atlas Stream Processing 在解析时拒绝任何其他连接类型。 |
| literal string | 必需 | 目标数据库的名称。 |
| literal string | 必需 | 包含 MongoDB 向量搜索索引的目标集合的名称。 |
| 字符串 | 必需 | 流式文档上用于搜索结果数组的输出字段。如果 MongoDB 向量搜索 未返回匹配文档,Atlas Stream Processing 会将此字段设置为空数组。 警告:如果此字段为空或与流元数据字段名称冲突,Atlas Stream Processing 将在启动时报告 |
| 数组 | 字段路径(Field Path)表达式 | 可选的 | |
| 文档 | 可选的 | |
| string | 字段路径(Field Path)表达式 | 可选的 | 用于自动嵌入的查询文本。 |
| 字符串 | Optional | 嵌入模型 MongoDB 向量搜索用于将 当您省略 |
| 字符串 | 必需 | 要查询的 MongoDB 向量搜索 索引的名称。 |
| 字符串 | 必需 | 目标集合中的字段,其中包含用于搜索的向量嵌入。 |
| int | 可选的 | MongoDB 向量搜索在搜索期间考虑的候选数量。除非 |
| int | 必需 | 要返回的最大结果数。不得超过 警告:如果 |
| 文档 | Optional | 在向量搜索之前应用的预筛选表达式。有关支持的操作符,请参阅筛选器。 警告:如果 |
| 布尔 | Optional | |
| 文档 | Optional | 包含 |
| 阵列 | Optional | Atlas Stream Processing 在将搜索结果附加到输入文档之前对其运行的其他聚合阶段。 |
| 文档 | Optional |
行为
查询模式
$vectorSearch 支持两种互斥的查询模式:queryVector 表示向量嵌入,query.text 表示自动嵌入。在任一模式下,您都可以指定字面值或字段路径(Field Path)表达式,以便单个流处理器可以为每个输入文档发出不同的查询。
Error Handling
如果目标 MongoDB 向量搜索 索引不存在,则当 $vectorSearch 首次运行时,流处理器将进入错误状态。Atlas Stream Processing 报告 MongoServerError 错误,并停止处理文档。
如果 queryVector 或 query.text 在运行时计算出的类型错误,Atlas Stream Processing仅将有问题的文档路由到死信队列(DLQ)(如果已配置),否则将其删除。流处理器会继续进程后续文档。有关 DLQ文档模式,请参阅 死信队列。
处理结果
在 Atlas Stream Processing 将搜索结果附加到输入文档之前,使用 pipeline 进一步处理搜索结果。Atlas Stream Processing 将 pipeline 原样转发到 MongoDB 向量搜索。要引用 pipeline 内的输入文档中的值,请通过 let 传递并将其引用为 $$<variable>。
示例
以下示例查询sample_mflix 数据集,其中包含有关电影和电影院的数据。要运行它们,请将示例数据加载到Atlas 集群,并在 集合上创建MongoDB Vectorembedded_movies Search索引。
以下示例使用电影推荐丰富入站搜索事件。每个输入文档都有一个 topic 字段。此管道使用 query.text,MongoDB 向量搜索使用 voyage-4-lite 模型为每个文档的 topic 值生成嵌入。此聚合分为三个阶段:
$source阶段与 Atlas 数据库建立连接,具体针对app数据库中的searches集合。fullDocumentOnly选项要求在源集合上启用前后图像。$vectorSearch阶段使用每个文档的topic字段中的文本查询sample_mflix.embedded_movies集合上的plot_vector_index索引,并将最多五个结果附加到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 阶段检索设立候选段落以最大限度地提高召回率,pipeline 中的 $rerank 阶段对这些候选段落重新排序,以便只有最相关的段落继续下游。此聚合有四个阶段:
$source阶段与 Atlas 数据库建立连接,具体针对llm_requests数据库中的events集合。fullDocumentOnly选项要求在源集合上启用前后图像。$vectorSearch阶段使用每个文档的queryEmbedding字段作为queryVector,在sample_mflix.embedded_movies集合上查询plot_embedding_index索引。它将numCandidates设置为高值以最大程度提高召回率,并将最多 50 个匹配项传递到pipeline。在pipeline内:query.text值引用let中定义的searchText变量,该变量可解析为每个文档的queryText字段。Atlas Stream Processing 将结果存储在context字段中。阶段会删除
$unsetqueryEmbedding和queryText字段,因为下游不再需要它们。$emit 阶段将富集文档发送到
qa_enriched主题。
重要
在运行使用 $rerank. 的管道之前,请确认您的 Atlas 项目管理员已启用“原生重排序”。
{ "$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.' } ... ] }