Training, evaluating and replacing a retriever
medium 6 minTrain a retrieval encoder on queries, useful passages and negatives.
Video lesson · Chapter 1 of 4
Change encoders while the shelves are moving
Theory
Read or listen first. About 2 min.
What it is
Train a retrieval encoder on queries, useful passages and negatives. A hard negative looks plausible under the current model but does not answer the query. False negatives are harmful: two differently worded documents can both be valid evidence. Split by source or time to avoid grading near-duplicates and measure separately on languages, rare names and exact identifiers.
Why it exists
A dual encoder lets document vectors be computed ahead of time. Late interaction preserves several vectors and compares local matches at query time. A cross-encoder jointly processes a query and each candidate, often as a reranking stage. Changing the encoder means the index representation changes too. A production migration needs evaluation, a backfill and ordered catch-up before the traffic switch.
Migrate the encoder while documents are changing
Inside this chapter
Chapter sources- Hard negatives and representation learning
- Contrast valid evidence with meaningfully confusing passages under the same query. Review ambiguous negatives, and use the documented query and passage encodings consistently. Sample the real distribution of languages and problem types.
A dual encoder learns by pulling each query towards its positive passage and pushing it away from negatives. Easy negatives teach little once the model is decent. Hard negatives, mined with the current model or BM25, teach the fine distinctions that matter, but they are also where false negatives hide: a passage that looks confusing may actually be a second correct answer. Review a sample before training.
Example. For “cancel a renewal,” a passage about cancelling a shipment is a useful hard negative. An alternate renewal policy for the same entitled customer may be another positive.
Training triples for cancel a renewal
Passage Label Why How to stop automatic renewal Positive Answers the query How to cancel a shipment Hard negative Shares cancel, wrong task Store opening hours Easy negative Obviously unrelated Renewal opt-out for enterprise plans Review: maybe positive Could also answer it Marking the last row negative would teach the model to push away valid evidence. - Candidate and reranker budgets
- Compare exact-neighbor retrieval, approximate indexing and relevance judgments to locate a regression. Rerankers can reorder only the retrieved candidates. Tune retrieval and reranking budgets against end-to-end quality and latency.
Each stage only sees what the stage before it passed on. To find where quality is lost, compare exact search with the approximate index (index loss), recall at the candidate budget (retrieval loss) and final ranking (reranker loss). Then tune budgets jointly: more candidates help recall but raise reranker latency roughly linearly.
Example. The correct part code arrives at rank 31 and the reranker sees 30 candidates. Optimizing the reranker alone cannot surface that result.
Why the reranker cannot fix rank 31
Raise the candidate budget or fix retrieval for identifiers; then retune the reranker. - Dual-index cutover
- Build the new index from a versioned snapshot while recording updates and deletions. Catch up, shadow real queries and compare cohorts and permissions. Switch an alias only after the candidate is current and rollback remains possible.
A new encoder means every document vector must be recomputed, and the corpus keeps changing while that runs. Backfill from a fixed snapshot, record every mutation after it, and replay them in order until the new index's watermark reaches the live log. Shadow real queries against both indexes, compare per slice and permissions, and only then move the read alias. Keep the old index until the new one has served real traffic without surprises.
Example. Index B reaches event 107, but an access change at 108 has already taken effect. Delay reads from B until access checks can honor it.
Switching to index B safely - 1Snapshot 100backfill B with new encoder
- 2Replay 101 to 108updates, deletes, ACL changes
- 3Watermark checkB caught up to live log
- 4Shadow readscompare A and B per slice
- 5Flip aliaskeep A for rollback
- Contrastive loss with in-batch negatives
- The usual training objective for dual encoders. For each query, its positive passage should score higher than every other passage in the batch, which double as free negatives.
The loss is the negative log of the probability that the model picks the positive out of the line-up. Using the other passages in the batch as negatives costs no extra encoding, which is why larger batches often help. The danger is duplicates: if two queries in a batch share a valid passage, the model is told to push that correct answer away. Deduplicate batches and add mined hard negatives for sharper distinctions.
Example. In a batch of 64 query and passage pairs, each query has 1 positive and 63 in-batch negatives, plus any mined hard negatives added to the batch.
InfoNCE for one query
s is usually cosine similarity and τ a temperature. The loss is a softmax cross-entropy over the positive and all negatives.
How it works
Step through the idea, or press Play.
Query pairs. Review confusing negatives
Understand it
Worked example
Migrate the encoder while documents are changing
- 1
Review confusing negatives
Hold out a multilingual test set and add labeled exact-code searches.
- 2
Backfill snapshot 100
Encode eligible documents with the same revision used for query encoding.
- 3
Replay events 101 through 108
Reconcile deletions and permission updates before comparing live results.
- 4
Check slices before cutover
Shadow eligible reads and measure retrieval plus end-to-end answer quality. Keep the incumbent available while evaluating the switch.
Follow the handoffs
Slice results before cutover
| Slice | Index A recall@20 | Index B recall@20 | Floor | Pass? |
|---|---|---|---|---|
| All queries | 0.82 | 0.87 | 0.80 | Yes |
| Non-English | 0.74 | 0.80 | 0.70 | Yes |
| Exact part numbers | 0.90 | 0.71 | 0.85 | No |
Index B lifecycle
Summary
The whole topic in one minute.
Equal dimensions do not imply equivalent representations. Aggregate improvements can hide a costly regression.
A dual encoder lets document vectors be computed ahead of time. Late interaction preserves several vectors and compares local matches at query time. A cross-encoder jointly processes a query and each candidate, often as a reranking stage. Changing the encoder means the index representation changes too. A production migration needs evaluation, a backfill and ordered catch-up before the traffic switch.