System Design
Design: Search Ranking
Learn Design: Search Ranking: 48 practice questions and answers with an interactive quiz — from the offline AI Learning app.
Questions & Answers
Tap a question to reveal its answer. Written for self-study and interview preparation.
What is the primary difference between BM25 and dense retrieval in search ranking?
BM25 is a sparse, lexical retrieval model that scores documents based on term frequency and inverse document frequency, excelling at exact keyword matching. It handles long-tail queries and rare terms well but fails on semantic queries where the document uses different vocabulary than the query. Dense retrieval uses bi-encoder neural networks (BERT-based) to embed queries and documents into a shared vector space, enabling semantic similarity search via approximate nearest neighbors. Dense retrieval handles paraphrases and synonyms but can miss exact rare terms and requires expensive GPU inference plus ANN indexing. Production search systems use a hybrid: BM25 retrieves based on keyword overlap, dense retrieval handles semantic queries, and a late-fusion step (Reciprocal Rank Fusion or learned re-ranker) combines both candidate lists. BM25 is near-zero latency at inference; dense retrieval requires vector index maintenance.
Design a search ranking system for an e-commerce platform with 500 million products and 100 million daily queries.
Requirements: sub-200ms end-to-end latency, high relevance and purchase conversion, handling of misspellings and synonyms. Pipeline: (1) Query understanding—spell correction, query classification (navigational vs informational), intent classification, named entity recognition (brand, category); (2) Retrieval—multi-source retrieval: BM25 index (Elasticsearch) for keyword matching, dense retrieval (bi-encoder + FAISS) for semantic similarity, business rules (curated landing pages); (3) Pre-ranking—lightweight gradient-boosted model scoring top-5000 candidates using sparse features; (4) Ranking—full LambdaMART or neural ranker (DNN) with cross-features: query-item relevance, behavioral CTR, user personalization signals, product popularity; (5) Re-ranking—apply diversity, business rules (promotion injection, brand constraints); (6) Serving: Elasticsearch cluster for BM25, GPU cluster for dense retrieval, feature store for real-time behavioral signals. Metrics: NDCG@10, purchase conversion rate, zero-result rate. Monitor: query latency p99, index freshness, and coverage of new products.
How do you implement query understanding in a production search system?
Query understanding transforms raw user queries into structured signals. Components: (1) Tokenization and normalization—lowercase, remove punctuation, handle Unicode; (2) Spell correction—statistical (Norvig algorithm, BK-tree) or neural (T5-based seq2seq) to correct typos before retrieval; (3) Query classification—intent classifier (navigational: user seeks a specific page, informational: seeking knowledge, transactional: seeking to buy); routes to different retrieval strategies; (4) Named entity recognition (NER)—identify brands, locations, product types in the query; use detected entities to constrain retrieval (category filter); (5) Query expansion—add synonyms from a curated vocabulary or from a learned word2vec/BERT model; (6) Semantic parsing—for complex queries, extract structured filters (color=red, price<$100); (7) Language detection—route multilingual queries to language-specific indexes. Latency: all query understanding must complete in under 20ms before retrieval starts. Serve from a lightweight FastAPI microservice with model quantization.
How does Learning to Rank (LTR) work and which approaches are used in production?
Learning to Rank trains models to produce a relevance ordering rather than a relevance score in isolation. Approaches: (1) Pointwise—train a regression or classification model to predict each document's relevance independently; simple but ignores list structure; (2) Pairwise—train on (query, doc_A, doc_B) triples to predict which document is more relevant; RankNet and RankBoost; (3) Listwise—optimize a ranking metric directly over the full result list; LambdaMART optimizes NDCG using gradient boosting with LambdaRank gradients; state-of-the-art for production. Feature engineering: query-document match features (BM25 score, field match), document quality features (pagerank, CTR history), personalization features, freshness. Models: LambdaMART (XGBoost/LightGBM with LambdaRank loss) is most widely deployed due to interpretability, speed, and competitive quality; neural LTR (DNN rankers) used when cross-feature interactions matter. Evaluation: offline NDCG@10 on human-labeled query-document pairs; online CTR and conversion lift.
How does a cross-encoder re-ranker differ from a bi-encoder retriever in neural search?
A bi-encoder encodes the query and document independently into separate embedding vectors, then computes similarity via dot product. Because document embeddings can be pre-computed and indexed offline, bi-encoders enable fast retrieval over millions of documents via ANN search. However, the query and document never interact in the encoder, limiting the model's ability to capture nuanced relevance. A cross-encoder takes the concatenated (query, document) pair as input and produces a single relevance score through full bidirectional attention. This allows the model to capture fine-grained query-document interactions (e.g., knowing that 'bank' in the query means financial institution when the document discusses loans). Cross-encoders are far more accurate but cannot be used for retrieval—they must score each candidate individually, making them suitable only for re-ranking a small set (top-100 to top-1000) of pre-retrieved candidates. Production architecture: bi-encoder for retrieval, cross-encoder (BERT or Electra fine-tuned) for re-ranking.
How do you handle personalized search ranking?
Personalized search adjusts result rankings based on individual user preferences and history. Architecture: (1) User representation—build a user embedding from click history, purchase history, browsed categories, and location; updated in real time via a feature store; (2) Personalized features—for each (user, query, document) triple, compute: historical CTR on this document or similar ones, number of past purchases in the document's category, user's preferred price range vs document price; (3) Integration into the ranker—append user features to the LambdaMART or DNN ranking model input alongside non-personalized features; (4) Context-aware personalization—apply stronger personalization for ambiguous queries (e.g., 'shoes') than for specific queries (e.g., 'Nike Air Max size 10'); (5) Privacy: anonymize user histories; respect opt-out signals. Evaluation: A/B test personalized vs. non-personalized; segment results by user cohort. Pitfall: new users have no history; fall back to popular or geographically relevant results.
How do you incorporate query expansion to improve search recall?
Query expansion adds related terms to a user's query to retrieve documents that use different vocabulary. Methods: (1) Synonym expansion—map query terms to synonyms using a curated thesaurus (WordNet) or learned synonym pairs from query-click logs; (2) Embedding-based expansion—use Word2Vec or FastText to find semantically similar terms in a learned embedding space; (3) Pseudo-relevance feedback (PRF)—retrieve the top-K documents for the original query, extract high-scoring terms from those documents, and add them to the query; (4) Query2Query expansion—find similar historical queries with high engagement and add their terms; (5) Large language model expansion—use an LLM to rewrite or expand the query with additional context before retrieval. Tradeoffs: aggressive expansion improves recall but hurts precision; expansion should be applied conservatively (only for low-recall queries identified by zero-result or low-CTR signals). Monitor: zero-result rate, query coverage, and whether expansion introduces topic drift that reduces relevance.
How do you build a spelling correction system for search queries?
Spelling correction recovers user intent from typos and phonetic mistakes. Architecture: (1) Statistical correction—noisy channel model: P(query_original | query_misspelled) ∝ P(misspelled | original) × P(original); candidate corrections generated from an edit-distance dictionary (Levenshtein ≤ 2), scored by language model probability; (2) Neural correction—seq2seq model (T5, BART) trained on (misspelled, corrected) pairs collected from search logs where users refined a query immediately after a zero-result query; (3) Candidate generation—BK-tree enables fast fuzzy lookup of dictionary words within edit distance 2; (4) Confidence threshold—only apply correction when the model is confident (probability of correction > 0.9); show original results alongside corrected results at lower confidence; (5) Domain-specific vocabulary—include brand names, product SKUs, and technical terms in the correction dictionary; standard spell checkers miss domain jargon. Evaluation: correction accuracy on a held-out dataset of labeled misspelled queries; A/B test against zero-result rate and CTR impact.
How is position bias measured and corrected in search click data used for training?
Position bias in search means documents shown at higher ranks receive more clicks regardless of their true relevance, because users focus attention on top results. If click data is naively used as training labels, the model learns to rank what was previously ranked highly rather than what is truly relevant. Measurement: randomization experiment—randomly swap pairs of documents between rank positions; compare click rates for the same document at different positions to estimate the position bias coefficient. Correction methods: (1) Examination hypothesis—P(click) = P(examined) × P(relevant|examined); estimate P(examined at rank k) from randomization data; divide click label by propensity; (2) IPS—weight each training sample by 1/P(shown at rank k); (3) Position-aware training—include rank as a feature at training time; set it to its mean value or exclude it at inference; (4) Eye-tracking data—use gaze patterns from user studies to estimate attention distribution across ranks. Validate: post-correction, predicted relevance should correlate with human relevance judgments independent of historical rank.
Design a search system for a code repository (like GitHub) with 300 million code files.
Code search has unique requirements: exact identifier matching, language-specific syntax, semantic code understanding. Pipeline: (1) Indexing—language-aware tokenization (split camelCase, snake_case identifiers, handle reserved keywords); inverted index per language; (2) Retrieval—keyword index (Elasticsearch) for identifier and file path search; dense retrieval using a code-specific encoder (CodeBERT, StarCoder embeddings) for semantic search; (3) Code-aware features: language, file path, function name, docstring, number of lines, programming language popularity, repository star count; (4) Neural re-ranker—CodeBERT cross-encoder fine-tuned on code search relevance data (query, code snippet) pairs from CodeSearchNet; (5) Personalization—user's primary programming language, recently viewed repositories; (6) Faceted filtering—language, license, and file type filters applied as hard constraints before ranking. Metrics: MRR@10 on CodeSearchNet benchmark; online CTR on code snippet results. Pitfall: generic text embeddings fail on code; use code-specific pre-trained models.
How do you handle zero-result queries in a search system?
Zero-result queries (queries returning no results) directly harm user experience and represent a missed conversion opportunity. Handling strategy: (1) Root cause analysis—classify zero-result queries: spelling errors, out-of-catalog queries, overly specific queries, empty index for new content categories; (2) Spelling correction—detect likely misspellings and automatically rewrite the query; (3) Query relaxation—systematically remove AND constraints and replace with OR; drop the least important query terms; use LLM to generate a simplified version of the query; (4) Fuzzy matching—lower BM25 minimum match threshold for zero-result queries; (5) Fallback index—serve from a broader fallback corpus (e.g., Wikipedia for informational queries) when the primary index has no results; (6) Suggestions—display 'Did you mean?' or related query suggestions; (7) Out-of-catalog detection—identify queries for products not yet in the catalog and route them to a product recommendation flow or wishlist feature. Monitor: zero-result rate as a key dashboard metric; set a target below 5% for mature search systems.
How do you design a multi-lingual search system?
Multi-lingual search must handle queries and documents in different languages, including cross-lingual scenarios where a French query matches an English document. Architecture: (1) Language detection—fastText language classifier on both queries and documents; (2) Language-specific indexes—maintain separate Elasticsearch indexes per language for lexical retrieval; (3) Cross-lingual dense retrieval—use multilingual encoders (mBERT, XLM-R, mE5) to embed queries and documents in a shared multilingual vector space; enables cross-language semantic matching; (4) Machine translation—optionally translate queries or documents into a pivot language (English) before retrieval; computationally expensive but effective; (5) Tokenization—language-specific tokenizers handle morphologically rich languages (German compound words, Arabic root morphology) better than English tokenizers; (6) Synonym dictionaries—maintain per-language synonym tables for query expansion. Evaluation: per-language NDCG and zero-result rate metrics; ensure no systematic quality degradation in low-resource languages. Staff native speakers to evaluate quality in each supported language.
How should freshness be incorporated into search ranking?
For queries where recency matters (news, product releases, events), freshness signals improve relevance. Incorporating freshness: (1) Freshness feature—include document publication date and last-modified date as input features in the ranking model; the model learns when freshness matters from click data (news queries show strong freshness preference); (2) Freshness decay multiplier—for time-sensitive query categories (detected via query classifier: 'latest iPhone', 'election results'), multiply base relevance score by exp(-lambda × age_hours); (3) Query-time freshness requirement—classify queries as time-sensitive or evergreen; apply strong freshness weighting only to time-sensitive queries; (4) Real-time indexing—ensure new documents (news articles, product updates) are indexed within seconds to minutes of publication; use incremental indexing (Elasticsearch near-real-time indexing); (5) Freshness vs quality tradeoff—very new documents have no engagement history; balance with content quality signals. Pitfall: applying freshness weighting to evergreen queries (how to bake bread) hurts relevance without benefit.
How do you design a search ranking system for a question-answering application?
QA search must retrieve passage-level answers, not just documents. Pipeline: (1) Dense passage retrieval (DPR)—bi-encoder fine-tuned on QA pairs (Natural Questions, TriviaQA); embed question and passages independently; retrieve top-100 passages via FAISS; (2) Reader model—extractive reader (BERT fine-tuned on SQuAD) highlights exact answer spans in retrieved passages; generative reader (T5, GPT) produces a free-text answer synthesized from multiple passages; (3) Answer ranking—score passages by a combination of retrieval score and answer extraction confidence; (4) Passage indexing—chunk long documents into overlapping 100-token passages for retrieval; store passages with metadata (source, section, URL); (5) Evidence fusion—aggregate evidence from multiple passages for multi-hop questions; (6) Confidence calibration—produce a well-calibrated confidence score to determine when to display an answer vs show a list of results. Metrics: Exact Match (EM) and F1 on QA benchmarks; user satisfaction surveys on answer quality. Pitfall: extractive reader fails on complex multi-hop questions requiring reasoning.
How do you collect and use implicit feedback (clicks) for training search ranking models?
Click logs are the primary training data source for search ranking at scale. Collection: log every query, result list (with ranks), clicks, and time spent on clicked URLs. Key considerations: (1) Position bias correction—clicks are heavily biased toward higher positions; apply IPS or randomized exploration to collect unbiased data; (2) Click model selection—UBM (User Browsing Model) or DBN (Dynamic Bayesian Network) estimate examination probability at each rank, enabling click-to-relevance conversion; (3) Dwell time filtering—apply a dwell time threshold (e.g., only count clicks resulting in 30+ seconds on the destination page) to filter 'satisfaction clicks' from accidental clicks; (4) Query normalization—normalize click rates by query frequency to prevent rare queries from being underrepresented; (5) Session context—model reformulation signals (query A followed by refined query B with clicks) as implicit relevance feedback for the original query. Label generation: use click-based relevance labels (SERP CTR) as noisy labels supplemented by human editorial judgments. Human labels are gold standard; click data provides scale.
How do you handle diversity in search result pages?
Search queries are often ambiguous: 'apple' could mean the fruit or the technology company. Diversity ensures the result page covers multiple interpretations. Approaches: (1) Query ambiguity detection—classify queries as ambiguous using a query analysis model; apply diversity only to ambiguous queries; (2) Intent clustering—cluster the candidate document set by subtopic or intent (using document embeddings or explicit topic labels); (3) MMR re-ranking—iteratively select documents maximizing relevance minus maximum similarity to already-selected documents; (4) xQuAD—probabilistic diversity framework that explicitly models query intents and selects documents covering different intents; (5) Intra-list similarity constraint—penalize result pages where consecutive results have cosine similarity above a threshold. Calibration: diversity trades off with raw relevance; tune the diversity coefficient lambda via A/B testing against engagement and satisfaction metrics. Pitfall: over-diversification for clear queries (unique brand search) confuses users expecting directly relevant results.
What is Reciprocal Rank Fusion and when should it be used in hybrid search?
Reciprocal Rank Fusion (RRF) is a simple, parameter-free method for combining ranked lists from multiple retrieval sources. For each document d across all ranked lists L, RRF score = sum over L of 1/(k + rank_L(d)), where k is a smoothing constant (typically 60). Documents appearing in the top ranks of multiple lists receive the highest combined scores. Advantages: no hyperparameter tuning required; robust to score scale differences between retrieval systems (BM25 scores and dense retrieval cosine similarities are not directly comparable); computationally trivial. Use RRF when: combining BM25 and dense retrieval results in a hybrid search pipeline; merging results from multiple specialized retrieval systems (text search + image search + knowledge graph). Limitation: RRF ignores score magnitudes entirely; a document with score 0.99 and a document with score 0.51 contribute equally if ranked at position 1. Learned fusion (a small MLP trained on the scores) outperforms RRF when labeled data is available.
How do you evaluate a search ranking system using human editorial judgments?
Human evaluation provides gold-standard relevance labels for queries and results. Process: (1) Judgment collection—trained raters assess each (query, document) pair on a 4-5 point scale: Perfect (navigational match), Excellent, Good, Fair, Bad; use standardized rating guidelines with examples; (2) Inter-rater agreement—compute Cohen's kappa or Krippendorff's alpha; target kappa above 0.7; adjudicate disagreements with a senior rater; (3) Query sampling—stratify sample by query frequency (head, torso, tail queries), query type (navigational, informational, transactional), and language; (4) Metrics—compute NDCG@10, ERR (Expected Reciprocal Rank), and MRR on the judged set; (5) Judgment coverage—the judged set covers a small fraction of all queries; use interpolation for unrated queries; (6) Adversarial query testing—include challenging queries (ambiguous, multi-intent, long-tail) that stress-test ranking quality. Refresh judgment pools quarterly as the document corpus and query distribution evolve. Complement human judgments with click-based signals for scale.
How do you design the indexing pipeline for a large-scale web search engine?
Web search indexing must process and serve billions of documents with freshness requirements. Architecture: (1) Crawling—distributed web crawler with politeness limits, robots.txt compliance, and priority-based crawling (high PageRank pages crawled more frequently); (2) Content extraction—parse HTML, extract clean text, title, anchor text, metadata; (3) Near-duplicate detection—MinHash LSH to detect and deduplicate near-identical content; (4) Indexing—inverted index construction mapping terms to posting lists (document IDs, TF, positions); sharded by term or document ID across thousands of machines; (5) Document quality signals—PageRank computation on the link graph using distributed iterative computation (Spark); spam detection; content quality classifier; (6) Real-time updates—incremental index updates for news and high-priority URLs via a separate hot index merged with the main index at query time; (7) Serving—distributed serving layer: query is broadcast to all index shards, results merged and ranked on a central aggregator. Freshness target: news documents indexed within 5 minutes.
How do you use query logs to discover emerging new queries and topics?
Query logs contain early signals of emerging trends before the broader content ecosystem has indexed relevant documents. Discovery pipeline: (1) Anomaly detection—monitor query frequency time series; flag queries with sudden growth exceeding 3 standard deviations above the 30-day average; (2) Cluster emerging queries—apply k-means or DBSCAN on query embeddings to discover new topic clusters; (3) Zero-result monitoring—track queries with zero results or very low CTR; these indicate gaps in the document corpus; (4) Query-to-content gap analysis—for trending queries, check if top-K retrieved documents match user intent (measure satisfaction CTR); low CTR with high query volume signals a content gap; (5) Entity extraction—identify new named entities (product releases, event names) appearing in queries; automatically enrich the search index vocabulary; (6) Feedback to content teams—surface content gaps to editorial or marketplace teams so they can create or source the missing content. Automate a weekly emerging-query report. Monitor the emerging query coverage rate as a search quality KPI.
How does document re-ranking using a cross-encoder work in a two-stage search pipeline?
In a two-stage pipeline, a fast retrieval stage (BM25 or bi-encoder) retrieves top-K candidates (typically 100–1000), and a cross-encoder re-ranker produces precise relevance scores for final ordering. Cross-encoder operation: concatenate query and document with special separator tokens ([CLS] query [SEP] document [SEP]), pass through a BERT-family model, use the [CLS] embedding as input to a relevance prediction head (linear layer + sigmoid). Training: fine-tune on human-labeled (query, document, relevance) triples using binary cross-entropy or pairwise hinge loss. Optimizations: (1) ONNX export and TensorRT for 3-5x inference speedup; (2) Batch inference—score all K candidates in a single forward pass batch; (3) Early exit—stop re-ranking at a confidence threshold once the top-N results are stable; (4) Distillation—distill a large cross-encoder (BERT-Large) into a smaller student (BERT-Mini) for 10x speedup with minimal quality loss. Latency budget: cross-encoder re-ranking of 100 candidates must complete in under 50ms.
How do you build a search system for long documents (research papers, legal contracts)?
Long documents exceed typical transformer context windows (512–8192 tokens). Strategies: (1) Passage-level indexing—segment documents into overlapping 200-token passages with stride 100; index and retrieve at passage level, then identify source document; (2) Hierarchical summarization—generate a document-level summary using a long-context LLM (Longformer, LED, Claude), embed the summary for document-level retrieval; (3) Document-level dense retrieval with long-context encoder—use Longformer or BigBird that handle 4096+ tokens; slower but avoids passage fragmentation; (4) Multi-vector retrieval (ColBERT)—represent each document as a set of token-level embeddings; at query time, compute MaxSim (maximum similarity across all document tokens for each query token); captures fine-grained matches without full cross-encoder scoring; (5) Keyword anchor points—for legal contracts, extract section headings and key defined terms as anchor features alongside passage embeddings. Evaluation: legal domain benchmarks (CUAD); research paper benchmarks (BEIR). Pitfall: passage retrieval can miss context that spans multiple passages (e.g., defined term in section 1 referenced in section 10).
What is ColBERT and how does it improve over standard bi-encoder retrieval?
ColBERT (Contextualized Late Interaction over BERT) is a neural retrieval model that encodes query and document independently (like a bi-encoder) but at the token level rather than producing a single embedding. The query encoder produces one embedding per query token; the document encoder produces one embedding per document token. At retrieval time, the relevance score is MaxSim: for each query token embedding, find the maximum cosine similarity with any document token embedding, then sum across all query tokens. This late interaction captures fine-grained token-level relevance without the full cross-attention of a cross-encoder. Benefits: more accurate than standard bi-encoder (captures multi-term matching, proximity); much faster than cross-encoder (document embeddings pre-computed); enables retrieval over millions of documents. Tradeoff: storage cost is high because every document token embedding is stored (roughly 100-300x more storage than bi-encoder's single embedding). Optimized version ColBERTv2 uses residual compression to reduce storage by 6-10x.
How do you tune and A/B test search ranking changes safely?
Search ranking changes can dramatically alter user experience. Safe experimentation protocol: (1) Offline evaluation first—compute NDCG, MRR, and user-study metrics on a held-out human-labeled query set; require improvement before online experiment; (2) Small A/B test (1-5% traffic)—randomize by user ID; primary metrics: CTR, task completion, dwell time; guardrail metrics: zero-result rate, bounce rate; (3) Duration—run for at least two weeks to account for day-of-week effects and novelty; (4) Statistical analysis—use z-test for proportion metrics; t-test for dwell time; apply Bonferroni correction for multiple metric comparisons; (5) Segment analysis—decompose results by query type (head/torso/tail), user type (new/returning), and language; improvements on tail queries may be masked in aggregate; (6) Side-by-side evaluation—for borderline results, run a user study showing Control and Treatment results to the same users. Roll out gradually (1% → 10% → 50% → 100%) with automated rollback triggers. Log all experiment assignments for reproducibility.
How do you build a visual search system that accepts image queries?
Visual search allows users to search using an image instead of text. Pipeline: (1) Image feature extraction—pass query image through a visual encoder (CLIP visual tower, EfficientNet, or a domain-specific CNN) to produce an image embedding; (2) Multi-modal index—product images in the catalog are pre-encoded and indexed in a FAISS vector store alongside text embeddings; (3) Cross-modal retrieval—compute cosine similarity between the query image embedding and catalog image embeddings; retrieve top-K visually similar items; (4) Optional text grounding—if the user also provides text (e.g., 'but in blue'), use CLIP's joint text-image embedding space to filter or re-rank; (5) Ranking—apply a cross-modal re-ranker that combines visual similarity, category match, price, and popularity; (6) Data augmentation—train with augmented product images (background removal, color jitter) to improve robustness. Evaluation: recall@K and precision@K on a test set of (query image, relevant product) pairs. Pitfall: visual similarity does not always equal user intent—a photo of a red dress may retrieve red sofas if the model focuses on color over category.
What is MRR (Mean Reciprocal Rank) and when is it the right evaluation metric for search?
Mean Reciprocal Rank (MRR) is the average of the reciprocal rank of the first relevant result across a set of queries: MRR = (1/|Q|) × sum(1/rank_i). It rewards systems that place at least one relevant result as high as possible. MRR is most appropriate when: (1) there is typically only one correct answer per query (navigational queries: user seeks a specific page; factoid QA: single correct answer); (2) finding the first relevant result is sufficient and additional relevant results add little value. MRR is less appropriate when: users benefit from multiple relevant results (informational queries, shopping); in these cases NDCG@K or MAP (Mean Average Precision) better capture the full ranking quality. Example: for a query 'OpenAI CEO', if the correct result is at rank 3, MRR = 1/3 = 0.33. For QA systems, MRR@10 on Natural Questions is a standard benchmark. MRR is easy to compute, interpretable, and well-suited for head queries where a single definitive answer exists.
How do you design a faceted search system with filtering for structured attributes?
Faceted search allows users to iteratively refine results using structured attribute filters (price range, color, brand, rating). Architecture: (1) Attribute indexing—store structured attributes in a separate column store (Elasticsearch keyword fields, Apache Solr facets) supporting fast aggregation queries; (2) Facet computation—after retrieval, aggregate attribute counts across the result set to populate filter panels ('Color: Red (142), Blue (89)'); (3) Filter application—apply user-selected filters as pre-retrieval constraints (Elasticsearch bool filter clause, applied before scoring for performance); (4) Dynamic facets—auto-detect which attributes are most discriminating for the query type (for 'laptop' queries, surface RAM, storage, brand; for clothing, surface size, color, material); (5) Multi-select facets—allow users to select multiple values within a facet (color: Red OR Blue); (6) Range facets—price, size, date as slider components with histogram distribution. Pitfall: computing facet counts over a billion documents is expensive; use approximate counting (HyperLogLog) or limit facet computation to the top-K retrieved results. Cache popular query facet responses.
How do you measure and optimize search result page quality using user satisfaction signals?
User satisfaction with search results is captured through both explicit and implicit signals. Signals: (1) Post-click dwell time—time spent on the clicked URL before returning to search; 30+ seconds suggests satisfaction; (2) Query reformulation rate—users who immediately retype a query signal dissatisfaction; measure reformulation within the same session; (3) Abandonment rate—sessions with no clicks (user found no relevant results); differs from productive no-click sessions (user found the answer in the snippet); (4) Return-to-SERP behavior—immediately clicking the back button signals dissatisfaction; (5) Task completion—for e-commerce, whether the user completed a purchase or add-to-cart within the session. Aggregate these into a satisfaction score per query-session. Use as training labels for a pointwise satisfaction model. A/B metric: QJSS (Query Journey Satisfaction Score)—composite of reformulation rate, dwell time, and task completion. Run periodic user studies to validate that implicit signals correlate with explicit satisfaction ratings from the same users.
What is the role of anchor text in web search ranking?
Anchor text is the visible, clickable text in a hyperlink pointing to another web page. It provides valuable editorial signals about a document's content from external perspectives. Role in ranking: (1) Content description—anchor text often describes the linked page's topic in natural language, providing additional vocabulary for indexing beyond the page's own text; (2) PageRank complement—anchor text improves the content signal while PageRank provides the authority signal; combined, they outperform either alone; (3) Query-anchor matching—pages whose inbound anchor texts match the query are likely highly relevant navigational targets; (4) Anchor text bag-of-words—aggregate all inbound anchor texts for a document into a pseudo-document for language model scoring; (5) Spam detection—anchor text can be manipulated (link farms with keyword-stuffed anchors); apply anchor text quality signals (source domain trust, diversity of linking domains). Limitation: anchor text is only available for documents with inbound links; new or obscure pages have sparse anchor data. Declining relevance as link-building practices evolve.
How do you design a semantic search system for an enterprise knowledge base?
Enterprise knowledge base search requires handling structured documents (wikis, policies, FAQs, Confluence pages) with access control. Architecture: (1) Document processing—chunk documents into passages of 200-300 tokens with overlapping context; extract metadata (author, department, last-modified date, access groups); (2) Embedding—use a general-purpose bi-encoder (E5, BGE, or a fine-tuned domain model) to produce passage embeddings; fine-tune on enterprise-specific query-answer pairs if available; (3) Access control enforcement—filter candidates by the querying user's access permissions before ranking; implement as a pre-retrieval metadata filter in the vector database (Pinecone, Weaviate, Qdrant); (4) Hybrid retrieval—combine BM25 on exact term matches with dense retrieval for semantic queries; RRF fusion; (5) Re-ranking—cross-encoder re-ranker fine-tuned on enterprise document domain; (6) Answer synthesis—optionally use an LLM (RAG pattern) to synthesize a direct answer from the top passages. Pitfall: document freshness—enterprise documents update frequently; incremental re-embedding pipeline required for modified documents.
How do you manage the indexing and retrieval of rapidly changing content (live sports scores, stock prices) in search?
Near-real-time content requires special indexing infrastructure that standard batch pipelines cannot support. Architecture: (1) Streaming indexing—publish updates to a Kafka topic; a dedicated streaming indexer (Elasticsearch with near-real-time settings, flush every 1 second) consumes the stream and updates the index; (2) Separate hot and cold indexes—maintain a hot index for live content (updated second-by-second) and a cold index for stable content; at query time, merge results from both; (3) Direct data API—for structured data like stock prices or sports scores, bypass text indexing entirely and call a live data API at query time when the query is classified as a live-data query; (4) TTL-based documents—set time-to-live on live content documents; expired documents automatically drop out of the index; (5) Query-time enrichment—detect queries requesting real-time data (entity + intent classifier), fetch live data from an API, and inject into the result page as a knowledge panel. Monitor: index freshness latency (time from event to search visibility) as a key SLA metric.
How do you detect and handle query intent ambiguity in search?
Ambiguous queries have multiple plausible interpretations: 'Mercury' (planet, element, car brand, NASA mission). Handling: (1) Ambiguity detection—train a multi-label intent classifier to detect when a query has multiple significant intents above a probability threshold; use query click entropy as a signal (high click spread across diverse documents indicates ambiguity); (2) Diverse result presentation—for ambiguous queries, ensure result page covers multiple intents using MMR or xQuAD diversity algorithms; (3) Disambiguation module—for very ambiguous queries, show a disambiguation panel asking the user to clarify ('Did you mean: Mercury (planet) or Mercury (car)?'); (4) Context-based disambiguation—use user history and session context to infer the most likely intent (a user who has been searching for cars is likely asking about Mercury the car brand); (5) Faceted clarification—present subtopic filters allowing users to drill down into a specific interpretation. Measure: track CTR distribution across different document types for the query; high diversity indicates successful ambiguity handling.
How do you implement vector search with filtering at billion-scale?
Vector search with metadata filtering (find the top-K most similar items that also match a filter predicate) is challenging at billion-scale because FAISS and most ANN indexes were designed for pure similarity search. Approaches: (1) Pre-filtering (filter then search)—apply metadata filters to reduce the candidate set before ANN search; only works when filters select a large fraction of the index (>10% of documents); small filtered subsets make ANN graph indexes inefficient; (2) Post-filtering (search then filter)—retrieve top-K×expansion_factor candidates via ANN, then filter by metadata; works when filters eliminate fewer than 90% of candidates; (3) Filtered ANN indexes—Weaviate and Qdrant implement HNSW with built-in filter awareness, pruning the graph traversal based on metadata; (4) Partitioned indexes—shard the vector index by a high-cardinality filter attribute (e.g., category, geography); route queries to relevant shards; (5) Two-column storage—store vector embeddings co-located with metadata in a vectorized column format for cache-efficient filtering. Choose strategy based on selectivity of the most common filters in production query traffic.
How do you handle long-tail queries (rare queries with few or no historical clicks) in search ranking?
Long-tail queries represent the majority of unique queries but have sparse or zero historical click data, making behavior-based ranking impossible. Strategies: (1) Generalization—group rare queries by semantic similarity to frequent queries using query embeddings; inherit click statistics from similar frequent queries; (2) Content-based ranking—for zero-data queries, rely entirely on BM25 and query-document term matching quality features without behavioral signals; (3) Transfer learning—fine-tune ranking models on head queries; generalize to tail via shared semantic features; (4) Query reformulation—automatically expand or reformulate tail queries to similar head queries with click data; (5) Zero-shot dense retrieval—semantic retrieval with strong pre-trained models (mE5, E5-large) generalizes better to unseen queries than BM25; (6) Prompt-based QA—for informational long-tail queries, use an LLM to generate a direct answer from retrieved documents instead of ranked results. Monitor: coverage rate (fraction of queries with at least 1 human-relevant result in top-10); long-tail queries often show the highest coverage gaps.
How do you build a search quality evaluation framework for a production search engine?
A comprehensive search quality evaluation framework combines offline, online, and qualitative signals. Components: (1) Offline evaluation suite—curated human-labeled query-document sets (500–5000 queries stratified by frequency); metrics: NDCG@10, MRR, precision@1; refresh quarterly; (2) Automated regression tests—run offline evaluation after every model update; block deployment if NDCG drops more than 0.5%; (3) Online A/B testing infrastructure—randomized user-level assignment; primary metrics (CTR, conversion, task success) and guardrail metrics (zero-result rate, bounce rate); (4) Error analysis dashboard—filter queries by low NDCG, zero-result, or high reformulation rate; surface worst-performing queries for analyst review; (5) Segment analysis—decompose evaluation metrics by query head/torso/tail, language, device type, and user cohort; (6) Comparative evaluation—blind side-by-side rating of control vs treatment results by trained raters for borderline A/B experiments; (7) Adversarial test suite—curated challenging queries (ambiguous, multilingual, temporal, domain-specific) for stress testing. Weekly quality review meeting with key metrics dashboard.
How do you use knowledge graphs to improve named entity search?
Knowledge graphs (KGs) like Wikidata and Google Knowledge Graph store structured facts about entities (people, places, organizations) enabling rich entity-centric search. Applications: (1) Entity recognition and disambiguation—identify entities mentioned in queries (NER + entity linking); resolve ambiguous mentions ('Jordan': person vs country vs brand) using KG context and user signals; (2) Entity embeddings—TransE, RotatE, or KGBERT embeddings for entities; use as search features for entity-centric ranking; (3) Knowledge panels—for navigational entity queries ('Elon Musk'), generate structured answer panels from KG facts (title, birth date, affiliations) without requiring document retrieval; (4) Related entity expansion—for a query about an organization, expand to include related entities (subsidiaries, executives) from the KG to improve recall; (5) Temporal freshness—KG entities should be updated when facts change (new CEO, company merger); alerts when KG facts diverge from document content. Pitfall: KGs have coverage gaps for niche entities and lag behind current events; supplement with real-time entity extraction from the live web.
What are the key differences between searching in a closed domain versus an open domain?
Closed-domain search operates over a fixed, curated corpus (company knowledge base, product catalog, legal documents), while open-domain search covers the entire web or an unbounded document collection. Key differences: (1) Corpus control—closed domain allows quality control of indexed documents; open domain requires aggressive spam filtering and quality scoring; (2) Query distribution—closed domain queries are predictable and domain-specific; open domain covers arbitrary user intents; (3) Personalization—closed domain (enterprise search) can tightly integrate organizational context (team, role, department); open domain personalizes from behavioral signals; (4) Model fine-tuning—closed domain benefits significantly from fine-tuning retrieval and ranking models on domain-specific labeled data; open domain requires generalizable pre-trained models; (5) Evaluation—closed domain evaluation is easier because a comprehensive document corpus can be fully labeled; open domain evaluation requires sampling strategies. (6) Latency—closed domain can afford more expensive models due to smaller corpus and smaller user base.
How do you monitor a production search ranking system for quality degradation?
Production monitoring for search quality requires a combination of real-time metrics and periodic audits. Monitoring layers: (1) Serving health—query latency (p50, p95, p99), error rate, cache hit rate, index freshness age; alert on p99 latency exceeding 500ms; (2) Engagement metrics—CTR, zero-result rate, reformulation rate, dwell time distribution; track daily and weekly trends; alert on greater than 5% CTR drop from the 7-day moving average; (3) Automated offline evaluation—nightly evaluation run on the human-labeled judgment set; alert if NDCG@10 drops more than 0.5% from baseline; (4) Log-based error analysis—daily sampling of worst-performing queries (lowest CTR, highest reformulation rate) for analyst review; (5) Coverage metrics—fraction of catalog indexed, new product indexing latency, language coverage completeness; (6) Anomaly detection—flag sudden changes in query distribution (emerging trends, attacks) that may cause model performance degradation. Weekly quality review with a stakeholder dashboard. Incident response playbook for common failure modes (index corruption, model serving failure, feature store outage).
How does SPLADE improve upon dense retrieval for search?
SPLADE (Sparse Lexical and Expansion Model for First Stage Ranking) combines the strengths of sparse retrieval (BM25's efficiency with inverted indexes) and dense retrieval (semantic understanding). SPLADE encodes queries and documents into sparse high-dimensional vectors where each dimension corresponds to a vocabulary term (typically 30,000+ dimensions). The encoder (BERT-based) learns to activate relevant vocabulary dimensions including terms not present in the input text (expansion)—a document about 'automobile' also activates 'car' and 'vehicle' dimensions. Key advantages: (1) Retrieval uses standard inverted index infrastructure (Elasticsearch-compatible), giving O(terms) retrieval vs O(N×D) for dense ANN; (2) Lexical control—exact term matching is preserved while semantic expansion improves recall; (3) Interpretability—sparse vectors show which terms drove the relevance decision; (4) No ANN infrastructure needed—inverted index with impact scores replaces FAISS. Tradeoff: SPLADE inference is slower than BM25 (requires BERT forward pass per document); documents are pre-processed offline. SPLADE consistently outperforms BM25 on BEIR benchmarks with near-dense-retrieval quality at sparse-retrieval infrastructure cost.
How do you design a search suggestion (autocomplete) system for a search bar?
Autocomplete suggests query completions in real time as the user types, reducing query formulation effort. Architecture: (1) Candidate generation—prefix-indexed trie over historical query completions; stored in Redis for sub-5ms lookup; filtered by frequency threshold (only suggest queries with more than N historical occurrences); (2) Ranking—rank candidates by: historical frequency, personalization (user's past queries), context (current session queries), trending score (recently spiking queries); (3) Neural completion—for unseen prefixes (new brand names, rare terms), a character-level or subword-level language model generates completions; (4) Latency—autocomplete must respond in under 50ms (target under 20ms) to maintain the interactive experience; use prefix hash caching for popular prefixes; (5) Diversity—ensure suggestions cover different intents and categories, not just the most popular completion; (6) Safety—filter offensive completions and legally sensitive suggestions from the suggestion list. Monitoring: suggestion acceptance rate (fraction of typed prefixes where user selects a suggestion), suggestion CTR, and zero-suggestion rate for new user-typed characters.
How do you use BEIR for evaluating zero-shot dense retrieval models?
BEIR (Benchmarking Information Retrieval) is a heterogeneous benchmark covering 18 diverse retrieval tasks: biomedical, financial, legal, news, question answering, and fact verification. It tests zero-shot generalization—models are evaluated on domains not seen during training. BEIR evaluation process: (1) Download task-specific corpora and query-relevance judgment files; (2) Encode all corpus documents with the retrieval model; index in FAISS; (3) Encode test queries; retrieve top-1000 candidates via ANN; (4) Compute NDCG@10 and Recall@100 against the provided relevance labels; (5) Average across all tasks for a single generalization score. Key findings: BM25 performs surprisingly well on BEIR (average NDCG@10 ~0.42); dense models trained on MS MARCO often fail on out-of-domain tasks (biomedical, legal); multi-task fine-tuned models (E5, BGE, Instructor) generalize significantly better. Use BEIR when selecting retrieval models for enterprise or specialized search to assess domain generalization before fine-tuning on domain-specific data.
How does contextual retrieval improve search quality for ambiguous or short queries?
Short queries (1-2 words) are highly ambiguous and carry little context. Contextual retrieval uses surrounding context to disambiguate and improve retrieval quality. Approaches: (1) Session context—include the user's recent search queries and clicked documents in the current session as additional context for the query encoder; ConvDR and ORCA use conversation-style encoding of session history; (2) User context—personalized query embeddings incorporate the user's long-term interest profile; (3) Contextual query reformulation—use the session history to rewrite the current query into a more specific form using a T5 or GPT model before retrieval; (4) HyDE (Hypothetical Document Embeddings)—use an LLM to generate a hypothetical ideal answer document for the query, embed the hypothetical document, and retrieve real documents similar to it; improves zero-shot retrieval on ambiguous queries; (5) Geographic context—use user location to resolve location-ambiguous queries ('best restaurant near me'). Evaluation: measure disambiguation accuracy on test sets of ambiguous queries with session context.
How do you implement click-through rate prediction as a feature for search ranking?
Historical CTR is a powerful ranking feature but requires careful handling to avoid position and selection biases. Implementation: (1) Query-URL CTR—for a (query, URL) pair, estimate CTR using Bayesian smoothing: CTR = (clicks + alpha × prior_CTR) / (impressions + alpha); prior is the URL's overall CTR across all queries; alpha controls smoothing strength; (2) Position-adjusted CTR—correct raw CTR for position bias using examination probability estimates before storing the value; (3) Temporal weighting—weight recent clicks higher; use exponential moving average with half-life of 30 days; (4) Segmentation—compute CTR separately for different user segments (new vs returning, mobile vs desktop) to provide a more personalized signal; (5) Cold start—for new URLs with no impressions, use the URL's domain-level CTR or category-level CTR as prior; (6) Integration—include adjusted CTR as a feature alongside query-document relevance features in the LTR model; do not use raw CTR as the sole ranking signal. Monitor: track whether CTR feature contributes meaningfully to ranking model; feature importance analysis.
How do you design a search system resilient to index poisoning and SEO spam?
SEO spam and index poisoning involve manipulating search rankings through low-quality content that tricks ranking signals. Defense architecture: (1) Link graph analysis—detect link farms (dense clusters of sites that only link to each other) using graph algorithms; penalize their PageRank propagation; (2) Content quality classifier—train a binary classifier distinguishing high-quality content from spam, thin content, or keyword-stuffed pages; features: text coherence, entity density, outbound link diversity, readability; (3) Hosting signal—penalize content from known bulk hosting providers, IP ranges associated with spam; (4) Historical behavior—track domain age, historical spam reports, prior penalties; (5) Cloaking detection—compare crawler-seen content versus browser-seen content; flag discrepancies as cloaking attempts; (6) Periodic spam sweeps—regular audit of top-ranked domains for quality; mass-demote detected spam farms; (7) User feedback—integrate explicit user feedback (mark as spam, low quality) as a training signal; aggregate feedback protects against isolated false reports. Monitor: fraction of top-10 results from new vs established domains; anomalous sudden ranking gains trigger manual review.
How do you build a distributed Elasticsearch cluster for search at scale?
Elasticsearch (ES) distributes search across nodes using a shard-based architecture. Design for scale: (1) Shard sizing—target 20–50GB per shard for balance between query performance and node overhead; too many small shards waste resources; (2) Replication—set replica shards to 1 or 2 for redundancy; replicas also serve read queries, improving throughput; (3) Node roles—separate master nodes (cluster coordination, 3 dedicated for quorum), data nodes (indexing and querying), and coordinating nodes (request routing, scatter-gather); (4) Index templates—define mappings, analyzers, and field types explicitly; avoid dynamic mapping which can cause mapping explosion; (5) Segment merging—tune merge policy for write-heavy (news indexing) vs read-heavy (product search) workloads; (6) Query optimization—avoid deep pagination (prefer search_after); use filters (cached) over queries for non-scoring constraints; (7) Cross-cluster search—for geographic distribution, use cross-cluster replication and cross-cluster search with local writes; (8) Monitoring—Elasticsearch metrics: indexing throughput, query latency, JVM heap usage, segment count; alert on JVM heap above 75%.
How do you handle the cold-start problem for new documents in a search ranking system?
New documents have no historical click data (CTR, dwell time), making behavior-based ranking impossible. Strategies: (1) Content-based ranking—rely on BM25 textual relevance and content quality features (text length, readability, structured data presence) as the primary signal; (2) Authority transfer—assign an initial quality score based on the document's domain authority (established domain = higher starting score); (3) Time-weighted injection—for news or product releases, apply a freshness boost in the first 24-48 hours to guarantee exposure for quality content; (4) Editorial curation—for strategically important new content (new product launches), manually curate its appearance in relevant query result pages; (5) Collaborative filtering from document metadata—if a new document is similar to existing high-CTR documents (same author, category, or topic), inherit a proxy CTR estimate; (6) Click data collection—rapidly accumulate initial click data through the freshness boost period; transition to behavior-weighted ranking once sufficient impressions accumulate (typically after 100+ impressions). Monitor: new document CTR vs established document CTR ratio as an indicator of cold-start effectiveness.
How do you apply transfer learning to improve search ranking for low-resource domains?
Low-resource search domains (legal, medical, niche e-commerce) lack large labeled query-document datasets for training ranking models. Transfer learning strategies: (1) Pre-trained encoders—initialize bi-encoders and cross-encoders from large pre-trained LMs (BERT, RoBERTa, DeBERTa) trained on massive text corpora; fine-tune on general-domain ranking data (MS MARCO) before domain-specific fine-tuning; (2) Intermediate domain training—find a richer intermediate domain that shares vocabulary with the target (biomedical → medical QA); fine-tune in sequence; (3) Zero-shot with domain-specific models—use models pre-trained on domain corpora (BioBERT for biomedical, LegalBERT for legal) as encoders without task-specific fine-tuning; (4) Data augmentation—use an LLM to generate synthetic (query, relevant passage) pairs from existing domain documents; use as weak training signal; (5) Cross-lingual transfer—for low-resource languages, transfer from a high-resource language using multilingual models (mE5, XLM-R). Evaluation: construct a small hand-labeled evaluation set (200–500 query-document pairs) for domain; track NDCG improvement with each transfer step.
What is the key insight behind Approximate Nearest Neighbor (ANN) methods like HNSW for dense retrieval?
Exact nearest neighbor search over billions of high-dimensional embeddings is O(N×D) per query—computationally infeasible at production scale. ANN methods trade a small amount of recall for dramatically faster search. HNSW (Hierarchical Navigable Small World) builds a multi-layer proximity graph: (1) Each item is a node connected to its k nearest neighbors; (2) The graph has multiple layers; higher layers have fewer nodes with longer-range connections (for fast coarse navigation); lower layers have dense local connections (for fine-grained search); (3) Search starts at the top layer, greedily navigating toward the query, then descends to lower layers for refinement. This enables logarithmic search complexity O(log N) instead of O(N). Key parameters: M (number of connections per node, controls graph density and memory), ef_construction (search width during index construction), ef (search width at query time, trades recall vs speed). HNSW achieves 95-99% recall at 10-100x speedup over exact search. In FAISS, HNSW is the highest-quality index type for static document collections.
Related topics in System Design
Put it into practice with AI Learning
This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.
- Free
- No account
- Works offline