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