RRF - Cosmos DB 中的查询语言(在 Azure 和 Fabric 中)

RRF 函数通过组合其他函数提供的两个或多个分数来返回融合分数。

Syntax

RRF(<function1>, <function2>, ..., <weights>)

Arguments

Description
function1 评分函数,如 VectorDistance 或 FullTextScore。
function2 评分函数,如 VectorDistance 或 FullTextScore。
weights 定义每个评分函数的重要性权重的数字数组。

返回类型

返回表示融合分数的数值。

例子

本部分包含有关如何使用此查询语言构造的示例。

混合搜索(矢量相似性 + BM25)

在此示例中,混合搜索结合了 FullTextScore 和 VectorDistance。

SELECT TOP 10 *
FROM c
ORDER BY RANK RRF(FullTextScore(c.text, "keyword"), VectorDistance(c.vector, [1,2,3]))
[
  {
    "id": "doc-042",
    "text": "The keyword appears frequently in this document about distributed systems.",
    "vector": [0.12, 0.87, 0.34]
  },
  {
    "id": "doc-119",
    "text": "Another relevant document mentioning the keyword in context.",
    "vector": [0.45, 0.22, 0.91]
  }
]

在此示例中,混合搜索使用评分函数的权重。

SELECT TOP 10 *
FROM c
ORDER BY RANK RRF(FullTextScore(c.text, "keyword"), VectorDistance(c.vector, [1,2,3]), [2,1])
[
  {
    "id": "doc-007",
    "text": "This document contains the keyword and is semantically close to the query vector.",
    "vector": [0.98, 0.11, 0.23]
  },
  {
    "id": "doc-355",
    "text": "A document with strong keyword relevance boosted by the higher weight.",
    "vector": [0.67, 0.44, 0.18]
  }
]

使用两个 FullTextScore 函数进行融合

在此示例中,将融合两个 FullTextScore 函数。

SELECT TOP 10 *
FROM c
ORDER BY RANK RRF(FullTextScore(c.text, "keyword1"), FullTextScore(c.text, "keyword2"))
[
  {
    "id": "doc-201",
    "text": "This article discusses both keyword1 and keyword2 in the context of data engineering."
  },
  {
    "id": "doc-088",
    "text": "A comprehensive overview that mentions keyword1 and covers keyword2 in detail."
  }
]

使用两个 VectorDistance 函数进行融合

在此示例中,将融合两个 VectorDistance 函数。

SELECT TOP 5 *
FROM c
ORDER BY RANK RRF(VectorDistance(c.vector1, [1,2,3]), VectorDistance(c.vector2, [2,2,4]))
[
  {
    "id": "doc-014",
    "vector1": [0.12, 0.87, 0.34],
    "vector2": [0.56, 0.78, 0.90]
  },
  {
    "id": "doc-092",
    "vector1": [0.45, 0.22, 0.91],
    "vector2": [0.33, 0.67, 0.45]
  }
]

注解

  • 此函数需要在 Azure Cosmos DB NoSQL 全文搜索功能中注册。
  • 混合搜索还需要在 Azure Cosmos DB NoSQL 矢量搜索中注册。
  • 此函数需要全文索引。
  • 此函数只能在子 ORDER BY RANK 句中使用,不能与其他 ORDER BY 属性路径结合使用。
  • 此函数不能是投影的一部分(例如, SELECT FullTextScore(c.text, "keyword") AS Score FROM c 无效)。