Reranking search results

Introduced 2.12

You can rerank search results using a cross-encoder reranker 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.

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.

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

Next, configure a search pipeline with a rerank processor.

The following example request creates a search pipeline with an ml_opensearch rerank processor. In the request, provide a model ID for the cross-encoder model and the document fields to use as context:

  1. PUT /_search/pipeline/my_pipeline
  2. {
  3. "description": "Pipeline for reranking with a cross-encoder",
  4. "response_processors": [
  5. {
  6. "rerank": {
  7. "ml_opensearch": {
  8. "model_id": "gnDIbI0BfUsSoeNT_jAw"
  9. },
  10. "context": {
  11. "document_fields": [
  12. "passage_text"
  13. ]
  14. }
  15. }
  16. }
  17. ]
  18. }

copy

For more information about the request fields, see Request fields.

Step 2: Create an index for ingestion

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:

  1. PUT /my-index
  2. {
  3. "settings": {
  4. "index.search.default_pipeline" : "my_pipeline"
  5. },
  6. "mappings": {
  7. "properties": {
  8. "passage_text": {
  9. "type": "text"
  10. }
  11. }
  12. }
  13. }

copy

Step 3: Ingest documents into the index

To ingest documents into the index created in the previous step, send the following bulk request:

  1. POST /_bulk
  2. { "index": { "_index": "my-index" } }
  3. { "passage_text" : "I said welcome to them and we entered the house" }
  4. { "index": { "_index": "my-index" } }
  5. { "passage_text" : "I feel welcomed in their family" }
  6. { "index": { "_index": "my-index" } }
  7. { "passage_text" : "Welcoming gifts are great" }

copy

Step 4: Search using reranking

To perform reranking search on your index, use any OpenSearch query and provide an additional ext.rerank field:

  1. POST /my-index/_search
  2. {
  3. "query": {
  4. "match": {
  5. "passage_text": "how to welcome in family"
  6. }
  7. },
  8. "ext": {
  9. "rerank": {
  10. "query_context": {
  11. "query_text": "how to welcome in family"
  12. }
  13. }
  14. }
  15. }

copy

Alternatively, you can provide the full path to the field containing the context. For more information, see Rerank processor example.