语义重排序

edit

此功能处于技术预览阶段,可能会在未来的版本中进行更改或移除。Elastic 将努力修复任何问题,但技术预览版中的功能不受官方 GA 功能支持 SLA 的约束。

本概述更侧重于语义重排序的高级概念和用例。有关如何在Elasticsearch中设置和使用语义重排序的完整实现细节,请参阅搜索API文档中的参考文档

重排序器提高了早期检索机制结果的相关性。 语义重排序器使用机器学习模型根据搜索结果与查询的语义相似性对其进行重新排序。

语义重排序需要相对较大且复杂的机器学习模型,并在响应查询时实时运行。 这种技术在小规模的top-k结果集上是有意义的,作为管道中的最后一步之一。 这是一种强大的技术,用于提高搜索相关性,适用于关键词、语义或混合检索算法。

接下来的章节将详细介绍语义重排序的好处、使用案例以及所使用的模型类型。 最后的章节包括一个实用的、高层次的概述,介绍如何在Elasticsearch中实现语义重排序,并提供了完整的参考文档链接。

用例

edit

语义重新排序支持多种用例:

  • 词法(BM25)检索结果重新排序

    • 通过在任何词法/BM25检索管道中添加一个简单的API调用,实现开箱即用的语义搜索。
    • 在不重新索引的情况下,为现有索引添加语义搜索功能,非常适合快速改进。
    • 非常适合具有复杂现有索引的环境。
  • 语义检索结果重排序

    • 使用更强大的模型,通过ELSER稀疏向量嵌入或密集向量嵌入来改进语义检索器的结果。
    • 在带有互惠排名融合(RRF)的混合检索之上添加一个细化层。
  • 通用应用

    • 支持自动和透明的分块,消除了索引时预分块的需求。
    • 在检索增强生成(RAG)用例或其他涉及语言模型(LLM)输入的场景中,提供对文档相关性的明确控制。

既然我们已经概述了语义重排序的价值,我们将探讨驱动这一过程的具体模型以及它们之间的差异。

交叉编码器和双编码器模型

edit

在较高层次上,用于语义重新排序的两种模型类型是:交叉编码器和双编码器。

在此版本中,Elasticsearch 仅支持跨编码器用于语义重新排序。

  • 一个交叉编码器模型可以被认为是一个更强大、一体化的解决方案,因为它生成了查询感知的文档表示。 它将查询和文档文本作为一个单一的、连接的输入。
  • 一个双编码器模型可以接受文档或查询文本作为输入。 文档和查询嵌入是分别计算的,因此它们彼此不了解。

    • 要计算排名分数,需要一个外部操作。这通常涉及在查询和文档嵌入之间计算点积或余弦相似度。

简而言之,交叉编码器提供高精度但更耗资源。 双编码器更快且更具成本效益但精度较低。

在未来的版本中,Elasticsearch 还将支持双编码器。 如果您对交叉编码器和双编码器之间的实际差异感兴趣,请展开下一节以获取更详细的分析。

跨编码器与双编码器之间的比较

以下是选择跨编码器和双编码器进行语义重排序时需要考虑的非详尽列表:

  • Because a cross-encoder model simultaneously processes both query and document texts, it can better infer their relevance, making it more effective as a reranker than a bi-encoder.
  • Cross-encoder models are generally larger and more computationally intensive, resulting in higher latencies and increased computational costs.
  • There are significantly fewer open-source cross-encoders, while bi-encoders offer a wide variety of sizes, languages, and other trade-offs.
  • The effectiveness of cross-encoders can also improve the relevance of semantic retrievers. For example, their ability to take word order into account can improve on dense or sparse embedding retrieval.
  • When trained in tandem with specific retrievers (like lexical/BM25), cross-encoders can “correct” typical errors made by those retrievers.
  • Cross-encoders output scores that are consistent across queries. This enables you to maintain high relevance in result sets, by setting a minimum score threshold for all queries. For example, this is important when using results in a RAG workflow or if you’re otherwise feeding results to LLMs. Note that similarity scores from bi-encoders/embedding similarities are query-dependent, meaning you cannot set universal cut-offs.
  • Bi-encoders rerank using embeddings. You can improve your re-ranking latency by creating embeddings at ingest-time. These embeddings can be stored for re-ranking without being indexed for retrieval, reducing your memory footprint.

Elasticsearch中的语义重排序

edit

在 Elasticsearch 中,语义重排器是使用 Elasticsearch Inference API检索器 实现的。

要在 Elasticsearch 中使用语义重排序,您需要:

  1. 选择一个重排序模型。 目前你可以:

  2. 使用 Elasticsearch Inference API 创建一个 rerank 任务。 Inference API 创建一个推理端点,并配置您选择的机器学习模型以执行重新排序任务。
  3. 在您的搜索请求中定义一个 text_similarity_reranker 检索器。 检索器语法使得在单个 API 调用中配置搜索结果的检索和重新排序变得简单。
示例搜索请求 使用语义重排序器

以下示例展示了一个使用语义重排器根据文档与查询的语义相似度重新排序前k个文档的搜索请求。

POST _search
{
  "retriever": {
    "text_similarity_reranker": {
      "retriever": {
        "standard": {
          "query": {
            "match": {
              "text": "How often does the moon hide the sun?"
            }
          }
        }
      },
      "field": "text",
      "inference_id": "my-cohere-rerank-model",
      "inference_text": "How often does the moon hide the sun?",
      "rank_window_size": 100,
      "min_score": 0.5
    }
  }
}

了解更多

edit