ML System Design

Design: Search Ranking

End to end architecture for building a search ranking system that handles queries, retrieves documents, and ranks results.

🔴 advanced8 min readsystem-designretrieval
Designing a Search Ranking System requires understanding queries, retrieving candidate documents, and ranking them by relevance. The pipeline starts with Query Understanding (spelling correction, intent classification, entity extraction), then Retrieval (BM25 lexical and dense vector hybrid search), followed by a learned Ranking model (Learning-to-Rank with NDCG optimization), and finally business rule re-ranking.

The Problem

Design a search system for a platform with 50 million documents. Users type short queries (2 to 4 words on average) and expect relevant results in under 200ms. The system must handle typos, synonyms, and ambiguous queries.

High-Level Architecture

  User Query: "cheep running shos nike"
              │
              ▼
  ┌───────────────────────────────┐
  │ STAGE 1: QUERY UNDERSTANDING  │  (~5ms)
  │ Spell Correct → Intent → NER  │
  │ Output: "cheap running shoes  │
  │          nike" + Brand=Nike    │
  └───────────────────────────────┘
              │
              ▼
  ┌───────────────────────────────┐
  │ STAGE 2: RETRIEVAL            │  (~10ms)
  │ BM25 + Dense Vector (Hybrid)  │
  │ Output: ~1000 Candidates      │
  └───────────────────────────────┘
              │
              ▼
  ┌───────────────────────────────┐
  │ STAGE 3: RANKING (L2R)        │  (~50ms)
  │ Cross-Encoder or GBDT Ranker  │
  │ Output: ~20 Scored Results    │
  └───────────────────────────────┘
              │
              ▼
  ┌───────────────────────────────┐
  │ STAGE 4: RE-RANKING           │  (~5ms)
  │ Freshness, Diversity, Ads     │
  │ Output: Final SERP Page       │
  └───────────────────────────────┘

Stage 1: Query Understanding

Before retrieving anything, clean up the raw query:

  1. Spelling Correction: Fix typos using edit distance or a masked language model ("cheep" becomes "cheap", "shos" becomes "shoes").
  2. Intent Classification: Is this navigational (go to a specific page), transactional (buy something), or informational (learn something)?
  3. Entity Extraction (NER): Pull out structured attributes like Brand=Nike, Category=Shoes, Color=Red.
  4. Query Expansion: Add synonyms ("shoes" also searches for "sneakers" and "footwear").

Stage 2: Retrieval

Run two retrieval methods in parallel and merge results:

  1. Lexical (BM25): Fast inverted index lookup. Great at finding exact product names, SKUs, and specific phrases.
  2. Semantic (Dense Vector): Encode the query with a bi-encoder, search the document vector index. Catches conceptual matches where words differ but meaning is the same.

Merge results using Reciprocal Rank Fusion (RRF): items that rank high in both lists get the highest combined score.

Stage 3: Ranking

A Learning-to-Rank model scores the 1000 candidates using rich features:

Popular model choices: LambdaMART (gradient boosted trees optimizing NDCG), or a Cross-Encoder Transformer for higher accuracy at the cost of more compute.

Stage 4: Re-Ranking

Apply business logic on top of model scores:

Training Data Collection

Search ranking models need relevance labels. Two main sources:

  1. Implicit Feedback: Click logs, dwell time (long clicks suggest relevance), query reformulations (user searching again suggests bad results).
  2. Human Judgments: Trained raters score query-document pairs on a scale like Perfect (4), Good (3), Fair (2), Bad (1), Terrible (0).

Combine both: use human judgments for model training and implicit signals for online monitoring.

Say this out loud

A search ranking system has four stages. Query Understanding cleans and enriches the raw query. Hybrid Retrieval combines BM25 for exact matches with dense vectors for semantic matches. A Learning-to-Rank model scores candidates using query, document, and user features. Re-ranking applies business rules like freshness and diversity before displaying results.

Followups to expect

  1. How do you handle zero-result queries? Progressively relax filters (drop Brand constraint, broaden category), fall back to semantic-only search, or show "did you mean" suggestions.
  2. How do you measure search quality online? Track metrics like Mean Reciprocal Rank of clicks, abandonment rate (zero clicks), and reformulation rate. Run A/B tests comparing ranking model versions.

Check yourself

Question 1 of 3

Why does a production search engine use hybrid retrieval combining BM25 and dense vector search?

More in ML System Design

See all →
A Framework for Any ML Design Round5 minFraming a Business Problem as ML5 minOnline vs Offline Evaluation5 min