{
  "id": "rerankers",
  "title": "Rerank Search Results",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/user_guide/how_to_guides/rerankers/",
  "summary": "",
  "tags": [],
  "last_updated": "2026-05-06T11:49:45+02:00",
  "page_type": "content",
  "content_hash": "ebae859c54ec1f2c48574270e01385a4e7bb069d11eba6e2bdcd1a08588e5baa",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide demonstrates how to use RedisVL to rerank search results (documents, chunks, or records) based on query relevance. RedisVL supports reranking through Cross-Encoders from [Hugging Face](https://huggingface.co/cross-encoder), [Cohere Rerank API](https://docs.cohere.com/docs/rerank-2), and [VoyageAI Rerank API](https://docs.voyageai.com/docs/reranker)."
    },
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "content",
      "text": "Before you begin, ensure you have:\n- Installed RedisVL: `pip install redisvl`\n- A running Redis instance ([Redis 8+](https://redis.io/downloads/) or [Redis Cloud](https://redis.io/cloud))"
    },
    {
      "id": "what-you-ll-learn",
      "title": "What You'll Learn",
      "role": "content",
      "text": "By the end of this guide, you will be able to:\n- Rerank search results using HuggingFace Cross-Encoders\n- Use the Cohere Rerank API with search results\n- Use the VoyageAI Rerank API for result reranking\n- Control the number of returned results after reranking\n\n\n[code example]"
    },
    {
      "id": "simple-reranking",
      "title": "Simple Reranking",
      "role": "content",
      "text": "Reranking provides a relevance boost to search results generated by\ntraditional (lexical) or semantic search strategies.\n\nAs a simple demonstration, take the passages and user query below:\n\n\n[code example]\n\nThe goal of reranking is to provide a more fine-grained quality improvement to\ninitial search results. With RedisVL, this would likely be results coming back\nfrom a search operation like full text or vector."
    },
    {
      "id": "using-the-cross-encoder-reranker",
      "title": "Using the Cross-Encoder Reranker",
      "role": "content",
      "text": "To use the cross-encoder reranker we initialize an instance of `HFCrossEncoderReranker` passing a suitable model (if no model is provided, the `cross-encoder/ms-marco-MiniLM-L-6-v2` model is used):  \n\n\n[code example]"
    },
    {
      "id": "rerank-documents-with-hfcrossencoderreranker",
      "title": "Rerank documents with HFCrossEncoderReranker",
      "role": "content",
      "text": "With the obtained reranker instance we can rerank and truncate the list of\ndocuments based on relevance to the initial query.\n\n\n[code example]\n\n\n[code example]\n\n    0.07461103051900864  --  {'content': 'Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.'}\n    0.052202966064214706  --  {'content': 'Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.'}\n    0.3802356719970703  --  {'content': 'Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.'}"
    },
    {
      "id": "using-the-cohere-reranker",
      "title": "Using the Cohere Reranker",
      "role": "content",
      "text": "To initialize the Cohere reranker you'll need to install the cohere library and provide the right Cohere API Key.\n\n\n[code example]\n\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "rerank-documents-with-coherereranker",
      "title": "Rerank documents with CohereReranker",
      "role": "content",
      "text": "The following example uses `CohereReranker` to rerank and truncate the list of\ndocuments based on relevance to the initial query.\n\n\n[code example]\n\n\n[code example]\n\n    0.9990564  --  Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\n    0.7516481  --  Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\n    0.08882029  --  The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan."
    },
    {
      "id": "working-with-semi-structured-documents",
      "title": "Working with semi-structured documents",
      "role": "content",
      "text": "Often times the initial result set includes other metadata and components that could be used to steer the reranking relevancy. To accomplish this, we can set the `rank_by` argument and provide documents with those additional fields.\n\n\n[code example]\n\n\n[code example]\n\n\n[code example]\n\n    0.9988121  --  {'source': 'textbook', 'passage': 'Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.'}\n    0.5974905  --  {'source': 'wiki', 'passage': 'Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.'}\n    0.059101548  --  {'source': 'encyclopedia', 'passage': 'The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.'}"
    },
    {
      "id": "using-the-voyageai-reranker",
      "title": "Using the VoyageAI Reranker",
      "role": "content",
      "text": "To initialize the VoyageAI reranker you'll need to install the voyaeai library and provide the right VoyageAI API Key.\n\n\n[code example]\n\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "rerank-documents-with-voyageaireranker",
      "title": "Rerank documents with VoyageAIReranker",
      "role": "content",
      "text": "The following example uses `VoyageAIReranker` to rerank and truncate the list of\ndocuments based on relevance to the initial query.\n\n\n[code example]\n\n\n[code example]\n\n    0.796875  --  Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\n    0.578125  --  Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\n    0.5625  --  Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274."
    },
    {
      "id": "next-steps",
      "title": "Next Steps",
      "role": "content",
      "text": "Now that you understand reranking, explore these related guides:\n\n- [Create Embeddings with Vectorizers](https://redis.io/docs/latest/vectorizers) - Generate embeddings using various providers\n- [Query and Filter Data](https://redis.io/docs/latest/complex_filtering) - Build complex filter expressions for search\n- [Use Advanced Query Types](https://redis.io/docs/latest/advanced_queries) - Learn about HybridQuery and other query types"
    },
    {
      "id": "cleanup",
      "title": "Cleanup",
      "role": "content",
      "text": "This guide does not create a persistent index, so no cleanup is required."
    }
  ],
  "examples": [
    {
      "id": "what-you-ll-learn-ex0",
      "language": "python",
      "code": "# import necessary modules\nimport os",
      "section_id": "what-you-ll-learn"
    },
    {
      "id": "simple-reranking-ex0",
      "language": "python",
      "code": "query = \"What is the capital of the United States?\"\ndocs = [\n    \"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.\",\n    \"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\",\n    \"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\",\n    \"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\",\n    \"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\"\n]",
      "section_id": "simple-reranking"
    },
    {
      "id": "using-the-cross-encoder-reranker-ex0",
      "language": "python",
      "code": "from redisvl.utils.rerank import HFCrossEncoderReranker\n\ncross_encoder_reranker = HFCrossEncoderReranker(\"BAAI/bge-reranker-base\")",
      "section_id": "using-the-cross-encoder-reranker"
    },
    {
      "id": "rerank-documents-with-hfcrossencoderreranker-ex0",
      "language": "python",
      "code": "results, scores = cross_encoder_reranker.rank(query=query, docs=docs)",
      "section_id": "rerank-documents-with-hfcrossencoderreranker"
    },
    {
      "id": "rerank-documents-with-hfcrossencoderreranker-ex1",
      "language": "python",
      "code": "for result, score in zip(results, scores):\n    print(score, \" -- \", result)",
      "section_id": "rerank-documents-with-hfcrossencoderreranker"
    },
    {
      "id": "using-the-cohere-reranker-ex0",
      "language": "python",
      "code": "#!pip install cohere",
      "section_id": "using-the-cohere-reranker"
    },
    {
      "id": "using-the-cohere-reranker-ex1",
      "language": "python",
      "code": "import getpass\n\n# setup the API Key\napi_key = os.environ.get(\"COHERE_API_KEY\") or getpass.getpass(\"Enter your Cohere API key: \")",
      "section_id": "using-the-cohere-reranker"
    },
    {
      "id": "using-the-cohere-reranker-ex2",
      "language": "python",
      "code": "from redisvl.utils.rerank import CohereReranker\n\ncohere_reranker = CohereReranker(limit=3, api_config={\"api_key\": api_key})",
      "section_id": "using-the-cohere-reranker"
    },
    {
      "id": "rerank-documents-with-coherereranker-ex0",
      "language": "python",
      "code": "results, scores = cohere_reranker.rank(query=query, docs=docs)",
      "section_id": "rerank-documents-with-coherereranker"
    },
    {
      "id": "rerank-documents-with-coherereranker-ex1",
      "language": "python",
      "code": "for result, score in zip(results, scores):\n    print(score, \" -- \", result)",
      "section_id": "rerank-documents-with-coherereranker"
    },
    {
      "id": "working-with-semi-structured-documents-ex0",
      "language": "python",
      "code": "docs = [\n    {\n        \"source\": \"wiki\",\n        \"passage\": \"Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.\"\n    },\n    {\n        \"source\": \"encyclopedia\",\n        \"passage\": \"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\"\n    },\n    {\n        \"source\": \"textbook\",\n        \"passage\": \"Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\"\n    },\n    {\n        \"source\": \"textbook\",\n        \"passage\": \"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\"\n    },\n    {\n        \"source\": \"wiki\",\n        \"passage\": \"Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\"\n    }\n]",
      "section_id": "working-with-semi-structured-documents"
    },
    {
      "id": "working-with-semi-structured-documents-ex1",
      "language": "python",
      "code": "results, scores = cohere_reranker.rank(query=query, docs=docs, rank_by=[\"passage\", \"source\"])",
      "section_id": "working-with-semi-structured-documents"
    },
    {
      "id": "working-with-semi-structured-documents-ex2",
      "language": "python",
      "code": "for result, score in zip(results, scores):\n    print(score, \" -- \", result)",
      "section_id": "working-with-semi-structured-documents"
    },
    {
      "id": "using-the-voyageai-reranker-ex0",
      "language": "python",
      "code": "#!pip install voyageai",
      "section_id": "using-the-voyageai-reranker"
    },
    {
      "id": "using-the-voyageai-reranker-ex1",
      "language": "python",
      "code": "import getpass\n\n# setup the API Key\napi_key = os.environ.get(\"VOYAGE_API_KEY\") or getpass.getpass(\"Enter your VoyageAI API key: \")",
      "section_id": "using-the-voyageai-reranker"
    },
    {
      "id": "using-the-voyageai-reranker-ex2",
      "language": "python",
      "code": "from redisvl.utils.rerank import VoyageAIReranker\n\nreranker = VoyageAIReranker(model=\"rerank-lite-1\", limit=3, api_config={\"api_key\": api_key})\n# Please check the available models at https://docs.voyageai.com/docs/reranker",
      "section_id": "using-the-voyageai-reranker"
    },
    {
      "id": "rerank-documents-with-voyageaireranker-ex0",
      "language": "python",
      "code": "results, scores = reranker.rank(query=query, docs=docs)",
      "section_id": "rerank-documents-with-voyageaireranker"
    },
    {
      "id": "rerank-documents-with-voyageaireranker-ex1",
      "language": "python",
      "code": "for result, score in zip(results, scores):\n    print(score, \" -- \", result)",
      "section_id": "rerank-documents-with-voyageaireranker"
    }
  ]
}
