Skip to content
mentoor

Topic 1 of 5 · 5 min read

Embedding and reranker models for search

Goal: Pick an embedding and rerank pair for a search feature and check it on a small labelled query set.

Choose an embedding model and a reranker by testing them on your own queries, not by their rank on a public leaderboard. An embedding model turns text into a vector so that similar meanings land close together. A reranker (a cross-encoder) reads a query and one candidate together and returns a relevance score. Neither writes text, and neither needs a chat model.

Check yourself · 1 of 3

What does a reranker do in a search pipeline?

Choose one answer

Why it matters

Search quality in a retrieval-augmented generation (RAG) app is capped by retrieval. If the right passage is not in the top results, no chat model can answer from it. Embeddings are also cheap to run, so the choice is mostly about fit: language, domain, vector size and cost.

How to choose

  1. Shortlist from a leaderboard. The MTEB leaderboard lists embedding models per task and language. Use it to pick three candidates, not one winner.
  2. Check the hard limits. Languages covered (an English-only model on Arabic support tickets fails quietly), maximum input length, vector dimensions (more dimensions mean more storage and slower search), licence and price per million tokens.
  3. Build a small labelled set. Collect 30 to 50 real queries and mark which document should come back for each. This takes an hour and beats any benchmark.
  4. Measure recall at k. For each query, is the right document in the top 5? Compare candidates on that single number.
  5. Add a reranker if recall at 50 is good but the top 5 is poor. Retrieve 50 candidates with the embedding model, then let the reranker reorder them. The reranker is slower per pair, so it only sees the shortlist.

Before and after

Weak: "We picked the top model on the leaderboard." The leaderboard average mixes tasks you do not have.

Better:

from sentence_transformers import SentenceTransformer, CrossEncoder

embed = SentenceTransformer("candidate-embedding-model")
rerank = CrossEncoder("candidate-reranker-model")

def hit_at_5(queries, docs, doc_ids, expected, use_rerank):
    doc_vecs = embed.encode(docs, normalize_embeddings=True)
    hits = 0
    for q, want in zip(queries, expected):
        scores = doc_vecs @ embed.encode(q, normalize_embeddings=True)
        top = scores.argsort()[::-1][:50]
        if use_rerank:
            pair_scores = rerank.predict([(q, docs[i]) for i in top])
            top = [top[i] for i in pair_scores.argsort()[::-1]]
        hits += want in [doc_ids[i] for i in top[:5]]
    return hits / len(queries)

Run it with and without the reranker, per candidate, and keep the cheapest pair that clears your target.

What goes wrong

  • Mixing models. Vectors from two embedding models cannot be compared. Switching models means re-embedding the whole corpus.
  • Ignoring input length. Text beyond the model's limit is cut off, so long documents are indexed by their first part only.
  • Reranking everything. Scoring every document with a cross-encoder is too slow; rerank only the top candidates.

Examples

The wrong language

A support team indexes Arabic and English tickets with an English-focused embedding model. English queries work; Arabic queries return unrelated tickets. Hit at 5 on 40 labelled Arabic queries shows the gap, and a multilingual model fixes it without any prompt change.

Retrieve wide, rerank narrow

Hit at 50 is high but hit at 5 is low. The right passage is retrieved, just ranked sixth or tenth. Adding a cross-encoder over the 50 candidates moves it into the top 5 without changing the index.

Key points

  • Embedding models turn text into vectors for search; rerankers score a query and a candidate together.
  • A leaderboard gives a shortlist; your own labelled queries decide the choice.
  • Check language coverage, input length, vector dimensions, licence and cost before testing quality.
  • Use a reranker on the top 50 or so candidates when the right document is retrieved but ranked too low.
  • Changing the embedding model means re-embedding the whole corpus.

Common mistakes

  • Picking the top leaderboard model without testing it on your own queries.
  • Embedding documents with one model and queries with another.
  • Forgetting that text past the input limit is silently dropped.
  • Using a chat model to rank search results when a reranker is cheaper and faster.

Check yourself · 2 of 3

You switch from one embedding model to another and keep the old vectors in the index. What happens?

Choose one answer

Check yourself · 3 of 3

Your recall at 50 is 95 percent but recall at 5 is 60 percent. Which change targets this gap?

Choose one answer

Practice items, not counted towards your mastery.

Start here