Lesson 2 of 4 Advanced Retrieval Techniques

Improving Relevance with Rerankers

This lesson explains how a reranker reads the query and each candidate passage together and scores relevance directly, improving on the fast but imprecise ranking of vector search.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish bi-encoders from rerankers.
  • Rerank results with Amazon Bedrock reranker models.
  • Decide when reranking helps and what it costs.

How rerankers work

An embedding model encodes the query and each document independently (a bi-encoder), so their interaction is reduced to a vector comparison. A reranker is a cross-encoder model that takes the query and a passage as one input and outputs a relevance score, capturing details such as negation or which entity a sentence is about. It is too slow to apply to millions of documents, so it is applied in a second stage to a candidate set returned by vector or hybrid search.

The two-stage pattern retrieves a generous candidate set (for example 30 to 100 chunks), reranks them, and passes only the top few (for example 5) to the FM. This improves the relevance of the context and reduces prompt tokens and cost at the same time.

The following diagram shows two-stage retrieval in which a reranker selects the best few of many candidates.

Two-stage retrieval with a reranker. Vector or hybrid search returns 40 candidates, a Bedrock reranker scores each together with the query, and only the top 5 go to the model. Recall at 40 was 0.94 but recall at 5 only 0.62, so reranking puts the right chunk in the prompt without making the prompt larger.
The recall figures come from the scenario above.
True or false

Bi-encoders and cross-encoders

Exam skills 1.5.4

Check three claims about how rerankers differ from embedding search.

Log in to try this activity Interactive activities are free with an account.

Reranking on Amazon Bedrock

Amazon Bedrock offers reranker models, such as Amazon Rerank and Cohere Rerank, in supported Regions. A reranker reads the query and each candidate passage together and scores how well the passage answers the query, which is more accurate than comparing separately computed embeddings, but too slow to apply to a whole collection. It is therefore applied to a small set of candidates retrieved first by vector or hybrid search.

Rerankers are used in two ways. In Bedrock Knowledge Bases, the Retrieve and RetrieveAndGenerate APIs accept a rerankingConfiguration that names the reranker model and the number of results to keep, and can choose which metadata fields are included in reranking. The standalone Rerank API reranks any list of text passages, so it also works with results from OpenSearch, pgvector or a keyword search engine, as in the following example.

import boto3
rt = boto3.client("bedrock-agent-runtime")
resp = rt.rerank(
    queries=[{"type": "TEXT", "textQuery": {"text": "Can contractors carry over unused leave?"}}],
    sources=[{"type": "INLINE", "inlineDocumentSource": {"type": "TEXT", "textDocument": {"text": t}}} for t in candidates],
    rerankingConfiguration={"type": "BEDROCK_RERANKING_MODEL", "bedrockRerankingConfiguration": {
        "numberOfResults": 5,
        "modelConfiguration": {"modelArn": "arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0"}}},
)
top = [candidates[r["index"]] for r in resp["results"]]

When reranking helps and what it costs

Reranking helps most when the right chunk is usually somewhere in the top 50 but not in the top 5, which retrieval evaluation reveals by comparing recall at 5 with recall at 50. It does not help when the right chunk is not retrieved at all; that needs better chunking, embeddings, hybrid search or query transformation. Reranking adds a model call with its own latency and cost per query, which is usually offset by sending fewer tokens to the generation model. The following table summarises the decision.

ObservationAction
Relevant chunk in top 50 but not top 5Add a reranker on a larger candidate set
Relevant chunk not in top 50Fix retrieval: chunking, embeddings, hybrid, query rewriting
Too many prompt tokens from many chunksRerank and keep only the top results
Strict latency budgetRerank a smaller candidate set or skip for simple queries
Scenario questions

Will reranking help?

Exam skills 1.5.4 · 5.1.6

Use retrieval metrics to decide whether a reranker is the right fix.

Log in to try this activity Interactive activities are free with an account.
Worked example

Relevant but low-ranked

Retrieval evaluation of an HR assistant shows recall at 5 of 0.62 but recall at 40 of 0.94. Answers are often incomplete.

The relevant chunks are being found but ranked too low to reach the prompt. Retrieving 40 candidates and applying a Bedrock reranker through the knowledge base reranking configuration, keeping the top 5, raises the share of prompts that contain the right chunk without increasing prompt size.

Check yourself

Why are rerankers applied to a candidate set rather than to the whole index?

Show answer

They score each query and passage together, which is accurate but too slow and costly to run on every document.

Exam Tips

Remember for the exam

  • Rerankers score query and passage together; apply them to a candidate set.
  • Retrieve many, rerank, keep few: better relevance and fewer prompt tokens.
  • Knowledge Bases: rerankingConfiguration; any source: the Rerank API.
  • Reranking cannot fix chunks that were never retrieved.