对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

如何执行混合搜索

A hybrid search is an aggregation of different search methods or search queries for the same or similar query criteria. This technique uses algorithms to rank results and return unified results from the different methods of search. You can use $rankFusion and $scoreFusion to perform a hybrid search.

倒数排名融合是一种技术,通过执行以下操作,将不同搜索方法的结果合并为一个结果集:

  1. 计算结果中各个文档的倒数排名。

    对于每个搜索结果中的每个排名文档,首先将该文档的排名 (r) 与常数 60 相加,以平滑分数 (rank_constant),然后将 1 除以 rrank_constant 之和,以得出该文档在搜索结果中的倒数排名。您无法设置 rank_constant 的值,默认值为 60

    reciprocal_rank = 1 / ( r + rank_constant )

    对于每种搜索方法,应用不同的权重 (w) 以使该搜索方法占更大重要性。对于每个文档,加权倒数排名的计算方法是将权重乘以文档的倒数排名。

    weighted_reciprocal_rank = w x reciprocal_rank
  2. 将结果中文档的派生排名分数和加权分数相结合。

    对于所有搜索结果中的每个文档,将计算出的倒数排名相加,获得文档的单个分数。

  3. 按结果中文档的综合得分对结果进行排序。

    根据结果中的综合得分对结果中的文档进行排序,以获得结果中文档的单一综合排名列表。

Relative score fusion is a technique to combine results from different search methods into a single result set by using the actual relevance scores from each pipeline — rather than the rank position of documents. This gives fine-grained control over how scores from different retrieval methods are normalized, weighted, and merged. You can use $scoreFusion to perform hybrid search using relative score fusion.

相对分数融合通过执行以下操作来组合结果:

将每个管道的分数进行规范化。

对于每个管道结果中的每个文档,使用标准化参数将原始分数标准化为常用扩展。扩展分数在不同检索方法之间存在很大差异——例如,向量搜索分数通常在 01 之间,而全文 BM25 分数可以高很多。标准化可以在组合之前将不同管道的分数带到可比较的扩展上。支持三种标准化方法:

  • none — 分数可以直接组合,无需要规范化。当所有管道已经以可比较的规模产生分数时使用。

  • sigmoid — 将 sigmoid 函数应用于每个分数,将分数映射到范围 (0, 1),而不考虑结果集中其他分数的分布:

    normalized_score = 1 / (1 + e⁻ˢ)
  • minMaxScaler — 应用最小-最大扩展,使用结果集中观察到的最小分数和最大分数,将最低分数映射到 0,将最高分数映射到 1

    normalized_score = (s - min_s) / (max_s - min_s)
将权重应用到每个管道的规范化分数。

对于每个管道,将每个文档的规范化分数乘以管道的权重 w。如果未指定,则权重默认为 1。使用权重使某种搜索方法比其他方法更受重视。

weighted_score = w x normalized_score
合并每个文档的加权分数。
对于出现在所有搜索结果中的每个文档,组合该文档出现所有管道的加权分数。默认组合方法是 avg,即平均加权分数。使用表达式定义自定义组合逻辑。
按综合得分对结果进行排序。
根据所有管道的索引结果中的综合得分对所有文档进行排序,以产生单一的统一排名列表。

您可以使用 MongoDB 向量搜索 执行多种类型的混合搜索。具体来说,MongoDB 向量搜索 支持以下使用案例:

  • Full-text and vector search in a single query: You can combine results from different search methods, such as a semantic and a full-text search. You can use the $vectorSearch for the semantic search and the $search for the full-text search results and combine the results by using the reciprocal rank fusion technique. To learn more, see the Perform Hybrid Vector and Full-Text Search tutorial, which demonstrates how to perform a semantic search and full-text search against the sample_mflix.embedded_movies namespace and retrieve combined ranked results by using reciprocal rank fusion.

    或者,对于更细粒度的混合搜索,其中除了结果的相对顺序之外,分数也很重要,您可以使用 $scoreFusion管道阶段。要了解更多信息,请参阅执行混合向量和全文搜索教程,该教程演示了如何对 sample_mflix.embedded_movies命名空间执行语义搜索和全文搜索,并将输入管道结果检索到最终评分结果集。

    $rankFusion 使用倒数排名融合算法根据文档在输入管道中的位置(相对排名)对文档进行排名,$scoreFusion 根据输入管道分配的分数对文档进行排名,并使用数学表达式组合结果。

    $rankFusion 中,排名受管道权重影响。在 $scoreFusion 中,权重控制每个管道的分数对最终结果的贡献。

    此外,要根据与查询的相关性对结果中的文档进行重新排序,可以在 $rankFusion$scoreFusion 阶段之后使用 $rerank 阶段。要了解更多信息,请参阅 $rerank 聚合管道阶段。

  • 在单个 MongoDB $rankFusion 管道中进行多个向量搜索查询:MongoDB 管道支持多个子管道,这些子管道包含针对同一集合执行的向量搜索查询,并使用倒数排名融合技术合并结果。如何组合多个 $vectorSearch 查询教程演示了以下类型的向量搜索:

    • 对您的数据集进行全面搜索,以在同一查询中查找语义相似的术语。

    • 在数据集中搜索多个字段,以确定哪些字段为查询返回最佳结果。

    • 使用不同嵌入模型生成的嵌入向量进行搜索,以确定不同模型在语义解释上的差异。

使用 $rankFusion$scoreFusion管道阶段进行混合搜索时,请考虑以下事项。

如果想捕获一种搜索方法无法捕获的漏报,从单个子管道获得不相交的结果可能是可以接受的。当结果不相交时,大部分或所有结果可能会显示为从一个管道返回,而不是从另一个管道返回。但是,如果希望所有子管道返回相似的结果,请尝试增加每个子管道的结果数量。

MongoDB 建议基于每个查询动态权衡词法查询和向量查询的权重,而不是对所有查询采用静态权重,以提高每个查询结果的相关性。这样还可以将资源分配给最需要的查询,从而提高计算资源的利用率。

You can combine an arbitrary number of sub-pipelines together in the $rankFusion or $scoreFusion stage, but they must all execute against the same collection. You cannot use the $rankFusion or $scoreFusion stage to search across collections. Use the $unionWith stage with $vectorSearch for cross-collection search.

MongoDB recommends using $match, $sort, and so on in your pipeline to boost on specific fields within your collection without requiring a search pipeline.

You can use the $geoNear and the near operator inside $search for a geographic location search within the $rankFusion or $scoreFusion stage. However, the $geoNear and the near operator use different coordinate reference frames. Therefore, the result ordinals and scores might not be identical.

MongoDB recommends setting limits for the number of results to return for each sub-pipeline. If the pipeline stage inside the $rankFusion or $scoreFusion input pipeline does not support limiting the number of results (such as $search), MongoDB recommends that you use $limit subsequently inside the input pipeline to limit the number of documents that $rankFusion or $scoreFusion evaluates.

MongoDB recommends using vectorSearch (MongoDB Search Operator) if you want to pre-filter data for vector search using $search operators that include analyzed text capabilities such as fuzzy search, phrase matching, location filtering, wildcard pattern matching, and so on.

以下限制应用于使用 $rankFusion$scoreFusion 的混合搜索:

  • $rankFusion 仅在 MongoDB 8.0 及更高版本(包括带自动升级功能的最新版本)上支持。$scoreFusion 在 v8.3 及更高版本上可用。

    注意

    要使用 $scoreFusion,您的集群必须运行 MongoDB v8.3 或更高版本。从 8.0 升级时,您可能必须暂停执行 $rankFusion 查询。

  • $rankFusion$scoreFusion 子管道只能包含以下阶段:

  • $rankFusion$scoreFusion 保留每个子管道的可追溯链接,可追溯到原始输入文档。因此,它们不支持以下内容:

  • $rankFusion$scoreFusion 子管道串行运行,而不是并行运行。

  • $rankFusion$scoreFusion 不支持分页。

  • rankFusion 只能在运行MongoDB.8 0或更高版本的集群上的视图上运行。不能在视图定义中或时间序列集合上运行rankFusion

要试用这些教程,必须具备以下条件:

  • MongoDB 版本 v8.0 或更高版本的集群。要使用 $scoreFusion,您的集群必须运行 MongoDB v8.3 或更高版本。

    注意

    对于自动嵌入,必须使用以下设置启用集群自动伸缩:

    • 如果当前集群层为 M10M20 (可爆发式 CPU 实例),请将最大实例大小设置为 M30 或更大。

    • 如果当前集群层为 M30 或更大,请将最大实例大小设置为大于当前层级的层级。

    • 对于使用 NVMe 存储的集群,请选择“扩展 NVMe 集群层”选项,当存储空间不足时。

    • 对于免费 (M0) 和 Flex 层级集群,无需进一步操作。

  • The Project Data Access Admin access to the project to create MongoDB Vector Search and MongoDB Search indexes.

  • The sample_mflix database loaded into your cluster. To learn more, see Load Sample Data.

  • mongosh 或MongoDB Compass创建索引并在集群上尝试查询。

    注意

    您还可以在使用Atlas CLI创建的本地Atlas部署和自托管(本地部署)部署中尝试这些混合搜索使用案例。要学习;了解更多信息,请参阅创建本地Atlas部署。