{
  "id": "rerankers",
  "title": "Rerank Search Results",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/0.16.0/user_guide/rerankers/",
  "summary": "",
  "content": "\n\nThis 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).\n\n## Prerequisites\n\nBefore 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))\n\n## What You'll Learn\n\nBy 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```python\n# import necessary modules\nimport os\n```\n\n## Simple Reranking\n\nReranking 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```python\nquery = \"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]\n```\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.\n\n### Using the Cross-Encoder Reranker\n\nTo 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```python\nfrom redisvl.utils.rerank import HFCrossEncoderReranker\n\ncross_encoder_reranker = HFCrossEncoderReranker(\"BAAI/bge-reranker-base\")\n```\n\n### Rerank documents with HFCrossEncoderReranker\n\nWith the obtained reranker instance we can rerank and truncate the list of\ndocuments based on relevance to the initial query.\n\n\n```python\nresults, scores = cross_encoder_reranker.rank(query=query, docs=docs)\n```\n\n\n```python\nfor result, score in zip(results, scores):\n    print(score, \" -- \", result)\n```\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.'}\n\n\n### Using the Cohere Reranker\n\nTo initialize the Cohere reranker you'll need to install the cohere library and provide the right Cohere API Key.\n\n\n```python\n#!pip install cohere\n```\n\n\n```python\nimport getpass\n\n# setup the API Key\napi_key = os.environ.get(\"COHERE_API_KEY\") or getpass.getpass(\"Enter your Cohere API key: \")\n```\n\n\n```python\nfrom redisvl.utils.rerank import CohereReranker\n\ncohere_reranker = CohereReranker(limit=3, api_config={\"api_key\": api_key})\n```\n\n### Rerank documents with CohereReranker\n\nThe following example uses `CohereReranker` to rerank and truncate the list of\ndocuments based on relevance to the initial query.\n\n\n```python\nresults, scores = cohere_reranker.rank(query=query, docs=docs)\n```\n\n\n```python\nfor result, score in zip(results, scores):\n    print(score, \" -- \", result)\n```\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.\n\n\n### Working with semi-structured documents\n\nOften 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```python\ndocs = [\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]\n```\n\n\n```python\nresults, scores = cohere_reranker.rank(query=query, docs=docs, rank_by=[\"passage\", \"source\"])\n```\n\n\n```python\nfor result, score in zip(results, scores):\n    print(score, \" -- \", result)\n```\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.'}\n\n\n### Using the VoyageAI Reranker\n\nTo initialize the VoyageAI reranker you'll need to install the voyaeai library and provide the right VoyageAI API Key.\n\n\n```python\n#!pip install voyageai\n```\n\n\n```python\nimport getpass\n\n# setup the API Key\napi_key = os.environ.get(\"VOYAGE_API_KEY\") or getpass.getpass(\"Enter your VoyageAI API key: \")\n```\n\n\n```python\nfrom 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\n```\n\n### Rerank documents with VoyageAIReranker\n\nThe following example uses `VoyageAIReranker` to rerank and truncate the list of\ndocuments based on relevance to the initial query.\n\n\n```python\nresults, scores = reranker.rank(query=query, docs=docs)\n```\n\n\n```python\nfor result, score in zip(results, scores):\n    print(score, \" -- \", result)\n```\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.\n\n\n## Next Steps\n\nNow that you understand reranking, explore these related guides:\n\n- [Create Embeddings with Vectorizers](04_vectorizers.ipynb) - Generate embeddings using various providers\n- [Query and Filter Data](02_complex_filtering.ipynb) - Build complex filter expressions for search\n- [Use Advanced Query Types](11_advanced_queries.ipynb) - Learn about HybridQuery and other query types\n\n## Cleanup\n\nThis guide does not create a persistent index, so no cleanup is required.\n",
  "tags": [],
  "last_updated": "2026-04-21T14:39:33+02:00"
}
