Skip to content

Search takes 40 milliseconds. Why are the first ten results wrong?

Search Returned an Answer. Was It the Right One? · Part 2 of 6 · 6 min read

Read the worked explanation ↓

Interview question. A marketplace has an Elasticsearch search cluster that meets a 50 ms p99 latency target. Product searches for "waterproof hiking boots" often return waterproof shoe spray, winter socks, and boots with no water resistance ahead of actual hiking boots. The team proposes a larger cluster and a higher BM25 boost. What would you investigate, measure, and change?

I would first decline the larger cluster. It might serve the same disappointing results faster. Latency tells us how efficiently the system answers its current ranking question. It does not tell us whether that question matches a shopper's intent.

Ask someone to explain the query path for "waterproof hiking boots" using one bad result. Which fields matched? Did the analyzer turn the three terms into tokens consistently? Did the query require all terms, accept any one, or boost title matches? Does a product named "Waterproof protection spray for hiking boots" satisfy the query better than a boot whose description contains the relevant technical specifications? Until we inspect a concrete score explanation, changing coefficients is guesswork.

Candidate retrieval and ranking are measured separately, with relevance judgments feeding back into the query and model rather than only latency dashboards.
Candidate retrieval and ranking are measured separately, with relevance judgments feeding back into the query and model rather than only latency dashboards.
Think first

A recruitment search platform ranks resumes for "senior backend engineer with payments experience". Design a judged query set, hard eligibility filters, lexical candidate retrieval and a second-stage ranker. Explain how to detect popularity bias and false relevance without relying only on clicks.

Take a few minutes to form an approach, then compare it with the worked explanation.

Reveal the worked explanation

Understand what BM25 does, and what it does not

BM25 is a lexical relevance formula. Roughly, a matching term contributes more when it is rare across documents, more when it appears several times within the document with diminishing returns, and less when its frequency is explained by a very long field. Its common implementation uses term frequency saturation, inverse document frequency and document length normalization. That is useful evidence about text matching, not proof the product solves the customer's task.

A rare word like waterproof may contribute more than a ubiquitous word like boots, depending on the corpus. A document can rank highly because it repeats the right terms while belonging to the wrong category. BM25 does not inherently understand the difference between boots and a spray for boots. Query structure, field analysis, category filters, product metadata, and sometimes semantic or learned ranking provide that distinction.

I would inspect the top twenty results with an explanation of each scoring component. Are title matches overweighted, are descriptions filled with repeated keywords, and are brands unintentionally dominating? Check tokenization, plural handling and synonym expansion. Broad synonyms are especially dangerous. Treating "hiking boot" and "shoe accessory" as equivalent does not help relevance just because those categories are related.

Build a test collection before tuning knobs

Select a few hundred real queries with coverage across head, medium and tail traffic, new products, languages, misspellings, and exact product codes. For each, have qualified reviewers judge candidate results on a stable scale such as 0 for irrelevant, 1 for loosely related, 2 for useful, and 3 for clearly satisfying the intent. Record why a result received its grade. A boot accessory may be related but should not receive the same grade as a waterproof hiking boot.

Now evaluate ranking quality offline. Precision@10 asks how many of the first ten are relevant. NDCG@10 rewards placing highly graded results earlier and compares the achieved ranking with an ideal ordering. Recall@K measures whether useful results enter the candidate set at all. That last metric is critical. If the retrieval stage never considers a relevant boot, no re-ranker can rescue it. Don't confuse a perfect re-ranking model with good end-to-end search when retrieval misses the answer.

Suppose a query has ten useful results in your judged pool, and candidate retrieval brings back only six of them in its first hundred. Even an ideal re-ranker of that hundred cannot produce all ten. That is a recall limitation, not a scoring-weight problem. Increasing candidate breadth may improve quality while increasing latency, memory and downstream ranking expense. That's a trade-off worth measuring.

Offline judgments have biases. Popular queries dominate if you sample purely by traffic. Human reviewers disagree, and a result may be useful for one shopper but not another. Keep separate evaluation slices for categories, languages and queries with strict constraints. Measure changes against a frozen baseline before deploying.

Separate eligibility from preference

Waterproofing might be a soft preference for some queries and a strict product attribute for others. If the customer selects the "Waterproof" filter, the ranker must not override that eligibility decision with a high text score. Availability, tenant visibility, moderation and regulatory restrictions may also be hard filters. An attractive score must not smuggle in an item the user is not allowed to buy or see.

After eligible candidate retrieval, combine signals deliberately. Exact phrase in a title, category match, verified attributes, lexical score, stock status and limited business signals can affect order. Use learning-to-rank or a second-stage semantic model where the gain justifies its cost. Watch for feedback loops. Ranking popular items higher increases clicks, and future click-trained models may then mistake exposure for intrinsic relevance. Counterfactual or randomized evaluation can help, but it must respect the product's risk and user expectations.

Online A/B tests come after offline safety checks. Track successful product views or purchases, query reformulation, zero-result rates, abandonment, and latency. Don't optimize raw click-through in isolation. Misleading thumbnails and irrelevant but sensational results can win clicks and lose trust. Inspect regressions in smaller but valuable query segments.

The answer I would expect in an interview

A strong engineer can describe a reproducible bad query, show how the index and analyzer generated candidates, explain what BM25 optimized, and produce a relevance dataset before proposing changes. They can identify the hard filters, budget candidate depth, and evaluate whether a second-stage ranker actually lifts useful results.

The central distinction is straightforward. Fast retrieval, good lexical matching, useful ranking and business success are four different measurements. A mature search system tracks all four because improving one can damage another. I'd ship a carefully tested query and ranking change before I bought more hardware to accelerate a relevance mistake.

Go deeper

Lucene's BM25 similarity includes a term rarity component such as log(1 + (N - df + 0.5)/(df + 0.5)), where df denotes document frequency. Exact scoring details depend on field statistics, query structure, analyzer and engine version. NDCG uses graded relevance with higher-ranked gains discounted. Evaluation pools can miss relevant items not surfaced by existing retrieval systems, so build judged collections through multiple retrieval methods and targeted adversarial queries rather than only logging the current top ten.

Check your reasoning

  • Inspect bad queries with analyzers, categories and scoring explanations.
  • Explain BM25 rarity, frequency saturation and length normalization.
  • Build graded judgments and representative query slices.
  • Measure recall@K before NDCG@K and distinguish second-stage ranking.
  • Keep hard eligibility/authorization filters and measure business outcomes.

Common mistake: Higher click-through proves more relevant results.

Search is fast but the top results are irrelevant. Which next step can localize and improve the failure?

Read the answer explanation

Measure judged candidate recall and ranked NDCG, then test safe changes — Offline graded judgments and candidate recall localize failures; online metrics should then check meaningful success and safety.

Apply it elsewhere

A code-search assistant retrieves files containing exact API names but ranks outdated examples above the canonical implementation. Define relevance labels and an evaluation protocol that tests version freshness separately from lexical score.

Compare your approach

Explain BM25 signals and limits accurately, separate candidate recall from ranking quality, propose graded judgments and NDCG@K, guard hard eligibility constraints, and justify online evaluation beyond simple CTR.

One more constraint

If none of the correct products occur in the top 200 lexical candidates, can a perfect reranker of those same 200 guarantee the correct product appears?

Quick revision

A 40 ms search can be wrong because speed and relevance are separate objectives. Inspect a real bad query, analyzers, field boosts and BM25 score explanations. Build a judged query set and track recall of retrieval candidates, NDCG@K of ranking, strict eligibility, and user outcomes. Re-rankers cannot recover items absent from the candidate set. Fix retrieval and filters first, then tune lexical or learned ranking with offline and online tests under explicit latency budgets.

References