Programming Fundamentals · Search and Sorting

Sorting's Real Face in AI: Rerank

Last lesson’s sort compared number size; in the AI world we rank “relevance”: RAG scoops a pile of passages from the knowledge base, and whoever ranks first squeezes into context—rank wrong, and the model will solemnly cite the wrong material. That “score + re-rank” step is Rerank. Today you’re on the job: play Rerank yourself.

Interactive · Play Rerank yourself

Scenario: a user asks the support bot “What’s your return policy?”, and coarse ranking scoops 5 candidate passages from the knowledge base. Each has three scores: vector similarity (close in meaning), keyword hit (literal match), freshness (how new the doc is). You’ve got three weight sliders; total = weighted average of the three. Watch: at first only similarity is heavy, so #1 is actually the 2023 outdated policy—crank up “freshness” and see the right clause climb to the top.

💬 What’s your return policy? Coarse ranking recalled 5 candidates
Core idea · Two-stage funnel

What you just did is “fine ranking” in a real system—the last layer of the funnel. The full flow looks like this (auto-plays when you scroll here):

0
All docs in the knowledge base
Coarse ranking: vector neighbors, fast and rough
0
candidate passages
Fine ranking Rerank: read one by one, costly and precise
0
finally stuffed into context
Why not fine-rank everything?

Expensive. Fine ranking feeds “question + passage” pairs into a model one by one. Do that for a million rows and you’ll wait an hour and burn a budget. So cheap coarse ranking cuts a million down to 50 first.

Why not coarse-rank everything?

Inaccurate. Coarse ranking only sees vector distance—it can’t tell “current policy” from “old policy,” as you just watched. So the last 50 need the costly model to gatekeep one by one.

Engineering = spend the right money at each funnel layer.Fast-and-rough algorithms run the open casting; costly-and-precise models run the finals—each stage does its job so total cost and total quality can both hit the mark. You’ll see this “spend by layer” idea again in caches, recommenders, and review systems.
Same pattern, more places · all “retrieve first, rank second”
🔍

Search result ranking

Search engines coarse-recall from trillions of pages, then fine-rank by relevance, authority, and freshness—the first page you see is Rerank’s output.

📱

Recommendation feed

Short video first coarse-filters thousands of candidates, then a fine-rank model scores “will you finish watching?” one by one. Your home feed order gets re-ranked dozens of times a day.

📚

RAG retrieval

Today’s home field: vector neighbors for a coarse scoop, Rerank for the fine pick—only the last few earn precious context. Remember last chapter’s O(n²) bill?

What this lesson wants to share