Reranking search results using a cross-encoder model使用交叉编码器模型对搜索结果进行重新排序

Introduced 2.12已引入 2.12

You can rerank search results using a cross-encoder model in order to improve search relevance. To implement reranking, you need to configure a search pipeline that runs at search time. The search pipeline intercepts search results and applies the rerank processor to them. The rerank processor evaluates the search results and sorts them based on the new scores provided by the cross-encoder model.您可以使用交叉编码器模型对搜索结果进行重新排序,以提升搜索相关性。要实现重新排序,需要配置在搜索时运行的搜索管道。该搜索管道拦截搜索结果并对其应用 rerank 处理器。rerank 处理器评估搜索结果,并根据交叉编码器模型提供的新分数对其进行排序。

PREREQUISITE
Before configuring a reranking pipeline, you must set up a cross-encoder model. For information about using an OpenSearch-provided model, see Cross-encoder models. For information about using a custom model, see Custom local models.
先决条件 在配置重新排序管道之前,必须先设置交叉编码器模型。有关使用 OpenSearch 提供的模型的信息,请参阅《交叉编码器模型》。有关使用自定义模型的信息,请参阅《自定义本地模型》。

Running a search with reranking使用重新排序进行搜索

To run a search with reranking, follow these steps:要使用重新排序进行搜索,请按以下步骤操作:

  1. Configure a search pipeline.配置搜索管道。
  2. Create an index for ingestion.为摄取创建索引。
  3. Ingest documents into the index.将文档摄取到索引中。
  4. Search using reranking.使用重新排序进行搜索。

Step 1: Configure a search pipeline步骤 1:配置搜索管道

Next, configure a search pipeline with a rerank processor and specify the ml_opensearch rerank type. In the request, provide a model ID for the cross-encoder model and the document fields to use as context:接下来,使用 rerank 处理器配置搜索管道,并指定 ml_opensearch rerank 类型。在请求中提供交叉编码器模型的模型 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"
          ]
        }
      }
    }
  ]
}

For more information about the request fields, see Request fields.有关请求字段的更多信息,请参阅《请求字段》。

Step 2: Create an index for ingestion步骤 2:为摄取创建索引

In order to use the rerank processor defined in your pipeline, create an OpenSearch index and add the pipeline created in the previous step as the default pipeline:为了在管道中使用已定义的 rerank 处理器,创建一个 OpenSearch 索引,并将上一步创建的管道设置为默认管道:

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

Step 3: Ingest documents into the index步骤 3:将文档摄取到索引中

To ingest documents into the index created in the previous step, send the following bulk request:要将文档摄取到上一步创建的索引中,请发送以下批量请求:

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" }

Step 4: Search using reranking步骤 4:使用重新排序进行搜索

To perform a reranking search on your index, use any OpenSearch query and provide an additional ext.rerank field:要在索引上执行重新排序搜索,使用任意 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"
      }
    }
  }
}

Alternatively, you can provide the full path to the field containing the context. For more information, see Rerank processor example.或者,您也可以提供包含上下文的字段的完整路径。更多信息请参阅《Rerank 处理器示例》。

Next steps后续步骤