1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
30 small wins to finish your pathNext lesson →
Reranking: a second, closer look at the top results
A reranker is a model that reads the question and a chunk together and scores their fit, reordering the top few chunks a retriever returned.
Last updated: 28 Sep, 2026 · LlamaIndex 0.14
An embedding compares question and chunk separately, so the ranking is approximate. A reranker reads them as one input and scores the match more precisely, which is why it runs only over a handful of retrieved chunks.
Loading a cross-encoder reranker
SentenceTransformerRerank uses a cross-encoder: a model that takes the question and a passage together and returns a relevance score. This small one downloads once, about 70 MB. top_n=2 keeps the best two after reordering.
from llama_index.core.postprocessor import SentenceTransformerRerank
from llama_index.core.schema import QueryBundle
reranker = SentenceTransformerRerank(
model="cross-encoder/ms-marco-MiniLM-L-2-v2", top_n=2
)Project files used on this pageThis lesson builds on a project from earlier lessons. The code below imports these files. Click a file to see its code, or follow the link to the lesson that wrote it. To run the code yourself, keep them in the same folder.
View the code here
help/lamps.md
# Lamps
The LMP-204 desk lamp has a known cable fault. Stop using a lamp with a damaged cable and we will replace it free of charge.
All lamps come with a two year guarantee against electrical faults.
Bulbs are not covered by the refund policy once they have been used.
The LMP-310 floor lamp needs a bulb with an E27 fitting, which is sold separately.
help/refunds.md
# Refunds
You can get a full refund within 30 days of delivery. The money goes back to the card you paid with within 5 working days of us receiving the item.
Items bought in a sale can be refunded too, but the delivery charge is not returned.
To start a refund, open the order in your account and choose Return an item. Print the label and drop the parcel at any post office.
Personalised items cannot be refunded unless they arrive damaged.
help/delivery.md
# Delivery
Standard delivery takes 3 to 5 working days and is free on orders over 40.
Express delivery arrives the next working day if you order before 2pm. It costs 6.
We deliver to the mainland only. Parcels to islands take 2 extra working days.
If a parcel has not arrived after 10 working days, contact us and we will send a replacement.
Reranking four retrieved chunks to two
from llama_index.core.postprocessor import SentenceTransformerRerank
from llama_index.core.schema import QueryBundle
reranker = SentenceTransformerRerank(model="cross-encoder/ms-marco-MiniLM-L-2-v2", top_n=2)
question = "How do I send an item back?"
candidates = index.as_retriever(similarity_top_k=4).retrieve(question)
print("retrieved:", [n.get_content().replace("\n", " ")[:45] for n in candidates])
reranked = reranker.postprocess_nodes(candidates, query_bundle=QueryBundle(question))
print("reranked: ", [(n.get_content().replace("\n", " ")[:45], round(float(n.score), 2)) for n in reranked])Output
retrieved: ['Print the label and drop the parcel at any po', 'If a parcel has not arrived after 10 working ', '# Refunds You can get a full refund within 3', '# Delivery Standard delivery takes 3 to 5 wo']
reranked: [('# Refunds You can get a full refund within 3', -2.3), ('Print the label and drop the parcel at any po', -10.19)]Reading the reranked order
- The retriever cast a wide net. Four chunks came back, with a printing-a-label chunk first and a delivery chunk second.
- The reranker reordered them. Reading each against the question, it moved the chunk about opening the order and choosing Return an item to the top and dropped the delivery chunk from the best two.
- Its scores are on a different scale. The cross-encoder's relevance scores can be negative; they order results and are not comparable to similarities.
Retrieval vs reranking
| Retrieval | Reranking | |
|---|---|---|
| Reads | Question and chunk separately | Question and chunk together |
| Speed | Fast over the whole index | Slow, over a handful |
| Role | Cast a wide net | Pick the best few |
When to add a reranker
- The right chunk is usually retrieved but not always first, and order matters for the prompt.
- You can retrieve generously, say 10 to 50 chunks, then narrow to the best 3 to 5.
- Precision of the top result matters more than the extra time per query.
Watch out
A reranker only reorders what it is given; it cannot recover a chunk retrieval missed. Retrieve widely enough that the right chunk is in the candidates, then rerank, and never treat its negative scores as similarities.
Related
- Previous: Hybrid search: keywords and meaning together
- Next: Measuring retrieval: hit rate on labelled questions
Try it yourself
- Rerank
"Which bulb does the floor lamp take?"and compare the gap between the scores. - Time the retrieval and the reranking separately with
time.perf_counter. - Add the reranker to a query engine through
node_postprocessors.
Slow is fine. Stopping is the only problem.