Link Search Menu Expand Document Documentation Menu

使用交叉编码器模型对搜索结果进行重新排序

2.12 版本引入

您可以使用交叉编码器模型对搜索结果进行重新排序,以提高搜索相关性。要实现重新排序,您需要配置一个在搜索时运行的搜索管道。搜索管道会拦截搜索结果并对其应用rerank 处理器rerank 处理器会评估搜索结果,并根据交叉编码器模型提供的新分数对其进行排序。

先决条件
在配置重新排序管道之前,您必须设置一个交叉编码器模型。有关使用 OpenSearch 提供模型的信息,请参阅交叉编码器模型。有关使用自定义模型的信息,请参阅自定义本地模型

运行带重排的搜索

要运行带重排的搜索,请遵循以下步骤:

  1. 配置搜索管道.
  2. 创建用于摄取的索引.
  3. 将文档摄取到索引中.
  4. 使用重排进行搜索.

步骤 1:配置搜索管道

接下来,配置一个带有rerank 处理器的搜索管道,并指定 ml_opensearch 重新排序类型。在请求中,为交叉编码器模型提供一个模型 ID,以及用作上下文的文档字段

PUT /_search/pipeline/my_pipeline
{
  "description": "Pipeline for reranking with a cross-encoder",
  "response_processors": [
    {
      "rerank": {
        "ml_opensearch": {
          "model_id": "gnDIbI0BfUsSoeNT_jAw"
        },
        "context": {
          "document_fields": [
            "passage_text"
          ]
        }
      }
    }
  ]
}

有关请求字段的更多信息,请参阅请求字段

步骤 2:创建用于摄取的索引

为了使用您管道中定义的rerank 处理器,请创建一个 OpenSearch 索引,并将上一步中创建的管道添加为默认管道

PUT /my-index
{
  "settings": {
    "index.search.default_pipeline" : "my_pipeline"
  },
  "mappings": {
    "properties": {
      "passage_text": {
        "type": "text"
      }
    }
  }
}

步骤 3:将文档摄取到索引中

要将文档摄入到上一步中创建的索引中,请发送以下批量请求

POST /_bulk
{ "index": { "_index": "my-index" } }
{ "passage_text" : "I said welcome to them and we entered the house" }
{ "index": { "_index": "my-index" } }
{ "passage_text" : "I feel welcomed in their family" }
{ "index": { "_index": "my-index" } }
{ "passage_text" : "Welcoming gifts are great" }

步骤 4:使用重新排序进行搜索

要在您的索引上执行重新排序搜索,请使用任何 OpenSearch 查询,并提供一个额外的 ext.rerank 字段

POST /my-index/_search
{
  "query": {
    "match": {
      "passage_text": "how to welcome in family"
    }
  },
  "ext": {
    "rerank": {
      "query_context": {
         "query_text": "how to welcome in family"
      }
    }
  }
}

或者,您可以提供包含上下文的字段的完整路径。有关更多信息,请参阅Rerank 处理器示例

后续步骤