重要
$rerank$rerank阶段使用 Voyage AI 的重新排名模型对输入文档重新排序,并返回按与查询的相关性排序的相同文档。$rerank阶段可以出现在聚合管道中的任何位置。 MongoDB建议您在$rerank$vectorSearch、$search、$rankFusion或$scoreFusion阶段之后使用 。
语法
$rerank管道阶段采用以下语法:
{ "$rerank": { "query": { "text": "<query-text>" }, "path": "<text-field-name>", "numDocsToRerank": <number-of-documents-to-rerank>, "model": "<reranker-model>" } }
字段
$rerank 阶段采用包含以下字段的文档:
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 对象 | 必需 | 用于重新排名的查询。 |
| 字符串 | 必需 | 用于重新排名的查询文本。示例,您可以指定:
|
| 字符串或字符串数组 | 必需 | |
| Int | 必需 | 发送到 Voyage AI进行重新排名并在结果中返回的最大文档数。根据管道定义的文档顺序选择文档。 最大值必须小于或等于 |
| 对象 | 必需 | 用于对文档重新排名的 Voyage AI 模型。值可以是以下之一:
|
Considerations
在使用 $rerank 之前,请查看要求、限制和 $rerank 行为。
要求
要使用 $rerank:
通过在Atlas用户界面Cluster Builder 页面中选择 Latest version with auto-upgrades,确保您的集群运行的是MongoDB 8.3 或更高版本。
通过Project Settings 启用原生重新排名。要学习;了解更多信息,请参阅启用或禁用原生重排名。
限制
不能使用 $rerank 阶段:
用于自管理或Atlas Local 部署。
对于
$rankFusion或$scoreFusion输入管道。如果视图上有MongoDB Search 或MongoDB Vector Search索引,请通过在源集合而不是视图上运行
.aggregate()来查询索引。
行为
$rerank可以出现在管道中的任何位置。但是, MongoDB建议在已按排序顺序返回相关文档的$rerank $search或 等阶段之后使用$vectorSearch 。
$rerank 重新排序并返回传递到此阶段的前 numDocsToRerank 个文档。如果 $rerank 是第一阶段,或者先前的阶段未返回确定性排序的结果,则用于 $rerank 的文档可能会在查询之间发生变化。
$rerank 如果 path 中指定的任何字段不存在于一个或多个输入文档中,则会返回错误信息。为了缓解这个问题,请使用:
查询
query.text字段确定重排序模型如何对每个文档进行评分。指定的 Voyage 重排序器模型在指定的 path处计算查询文本与每个文档的内容之间的相关性分数。对于大多数使用案例,请将 设立为与前面的$rerank.query.text $search或$vectorSearch 阶段中的查询相同或相似的文本。
评分
使用 score 变量和 $meta 表达式检索$rerank 阶段结果中每个文档的分数。
{ "$addFields": { "rerankScore": { "$meta": "score" } } }
$rerank 阶段将 $meta: "score" 的值替换为新分数。要保留 $rankFusion 等前一阶段的值,您可以在 $rerank 阶段之前将分数项目到命名字段中。
{ "$addFields": { "originalRankFusionScore": { "$meta": "score" } } }, { "$rerank": { ... } }, { "$addFields": { "rerankScore": { "$meta": "score" } } }
性能和准确性
$rerank阶段的计算成本可能很高,具体取决于输入的大小,例如要重新排名的字段、要重新排名的文档数量以及查询文本。$rerank 在查询时联合处理查询和文档。因此,$rerank 可能比由...提供支持索引(例如$search 或$vectorSearch 的索引)支持的相关性搜索慢。要平衡准确性和性能,请配置numDocsToRerank 和path $rerank以限制输入大小。 不会在您的Atlas 集群资源上运行Voyage AI重排名模型。
$rerank 最适合检索质量优先于超低延迟的工作负载,例如 RAG 和代理AI应用程序。对于需要超低延迟的工作负载, MongoDB建议使用 $search 或 $vectorSearch 等索引驱动的搜索阶段。
例子
以下示例演示了如何使用 $rerank 阶段根据 Voyage AI重排序器模型对 sample_mflix.embedded_movies集合中的文档重新排序。查询在$match阶段之后使用 $rerank,以使用 rerank-2.5 重新排序器模型对文档重新排序。在以下聚合管道中:
$match阶段筛选文档,以仅包含具有 string 类型的plot字段的文档。$sort阶段按released字段的降序对文档进行排序,以确保确定性的排序。$rerank阶段使用rerank-2.5重排序器模型对文档重新排序以匹配查询。$addFields阶段将名为rerankScore的字段添加到文档中。
1 db.embedded_movies.aggregate([ 2 { 3 "$match": { 4 "plot": { "$exists": true, "$type": "string" } 5 } 6 }, 7 { 8 "$sort": { "released": -1 } 9 }, 10 { 11 "$rerank": { 12 "model": "rerank-2.5", 13 "query": { 14 "text": "a group of heroes band together to stop a powerful enemy and save the world" 15 }, 16 "numDocsToRerank": 100, 17 "path": ["title", "plot"] 18 } 19 }, 20 { 21 "$addFields": { 22 "rerankScore": { "$meta": "score" } 23 } 24 }, 25 { "$limit": 10 }, 26 { 27 "$project": { 28 "_id": 0, 29 "title": 1, 30 "plot": 1, 31 "rerankScore": 1 32 } 33 } 34 ])
[ { plot: 'No treason, no surrender.', title: 'Ti mene nosis', rerankScore: 0.5986876487731934 }, { plot: 'The life of the greatest karate master of a generation.', title: 'The Real Miyagi', rerankScore: 0.5986876487731934 }, { plot: 'A shy genius is employed by his former university to design robot software.', title: 'Eva', rerankScore: 0.5986876487731934 }, { plot: 'The owners of a failing security company start robbing houses to boost business.', title: 'Armed Response', rerankScore: 0.5986876487731934 }, { plot: 'A live telecast of the beloved J. M. Barrie story.', title: 'Peter Pan Live!', rerankScore: 0.5986876487731934 }, { plot: "A French police magistrate spends years trying to take down one of the country's most powerful drug rings.", title: 'The Connection', rerankScore: 0.5986876487731934 }, { plot: 'A documentary that follows undercover activists trying to stave off a man-made mass extinction.', title: 'Racing Extinction', rerankScore: 0.5986876487731934 }, { plot: 'An ex-hitman comes out of retirement to track down the gangsters that took everything from him.', title: 'John Wick', rerankScore: 0.5986876487731934 }, { plot: 'A former hit-man for a drug cartel becomes a vigilante to pay for his sins and find redemption.', title: 'Redeemer', rerankScore: 0.5986876487731934 }, { plot: 'Charles Ingvar Jènsson gathers three criminals to take vengeance upon the people who killed his uncle.', title: 'The Master Plan', rerankScore: 0.5986876487731934 } ]