SD System Design In Depth
0%
lesson system-designsearch

Query understanding pipeline

Module 5.4: Information Retrieval & Search Engines Updated Sep 17, 2026
📄 Cheat Sheet
Berlin Buzzwords 2018: Giovanni Fernandez-Kincade – Getting Started with Query Understanding #bbuzz
Plain Schwarz Berlin Buzzwords 2018: Giovanni Fernandez-Kincade – Getting Started with Query Understanding #bbuzz Watch on YouTube ↗

A practitioner talk (Etsy search) on spelling, segmentation, entity tagging, classification and rewriting, laid out as a pipeline exactly like this unit.

0:00 / --:--

Under the Hood: Bridging the Vocabulary Mismatch

Users express search intent using incomplete, misspelled, or ambiguous queries (e.g. "cheap nike shoes red size 10"). A naive literal keyword search against an inverted index frequently returns zero results. The Query Understanding Pipeline transforms raw user queries into rich structured search intents before querying the index.

The Multi-Stage Query Understanding Flow

flowchart TD Raw["Raw User Query: 'red nkie runnign shoe'"] --> Spell["1. Spelling Correction: 'red nike running shoe'"] Spell --> Tokenize["2. Tokenization & Normalization"] Tokenize --> NER["3. Named Entity Recognition (NER)"] subgraph EntityExtraction ["Entity Mapping"] NER --> Brand["Brand: 'Nike'"] NER --> Category["Category: 'Running Shoes'"] NER --> Color["Attribute: Color = 'Red'"] end NER --> Intent["4. Intent Classifier: Commercial Purchase"] Intent --> Rewrite["5. Query Rewriting & Expansion (Synonyms: 'sneakers', 'trainers')"] Rewrite --> Structured["6. Structured Query: filter(brand='Nike', cat='Shoes') & match('red running')"]

Under the Hood: Machine Learning at the Query Boundary

  • Named Entity Recognition (NER): Lightweight transformer models (DistilBERT) or fast linear CRF models identify brand names, product models, sizes, and colors in sub-10 milliseconds.
  • Synonym Expansion: Expands query terms using offline word embedding graphs (e.g. mapping "hoodie" to "sweatshirt"). Expansion weights are discounted (weight = 0.5) so exact matches rank higher than synonym matches.
  • Query Relaxation: If a strict boolean query yields zero results, the pipeline automatically relaxes constraints (e.g. dropping non-essential adjective tokens) while warning the user: "No exact match found; showing results for Nike shoes".

Worked example: one query through the pipeline

Raw input: red nkie runnign shoes size 10

StageOutputTypical budget
Normaliselowercase, trim, Unicode foldunder 1 ms
Spell correctred nike running shoes size 101 to 3 ms
Segment and tagcolour=red, brand=nike, category=running shoes, size=102 to 10 ms
Classify intentproduct purchase, category footwear, confidence 0.932 to 5 ms
Rewritefilter brand=nike and category=footwear, boost colour=red and size=10, text match running shoes with synonym trainers at weight 0.5under 1 ms

Notice the split between hard filters (high-confidence brand and category) and soft boosts (colour, size). If the shop has Nike running shoes in size 10 but none in red, the user still sees results rather than an empty page.

flowchart LR A["Raw query"] --> B["Spell correct"] B --> C["Tag entities"] C --> D["Classify intent"] D --> E["Rewrite to filters and boosts"] E --> F["Retrieve"] F --> G{"Zero results?"} G -->|"Yes"| H["Relax: drop lowest confidence constraint"] H --> F G -->|"No"| I["Rank and return"]

Trade-offs

ChoiceUpsideDownside
Dictionary and rulesFast, explainable, easy to hot-fixPoor coverage of the long tail
ML taggers and classifiersGeneralise to unseen phrasingNeed labelled data; silent errors
LLM rewritingHandles complex natural languageLatency and cost; must be cached or used only on tail queries

Failure modes: over-correction ("iphone" corrected to "phone"), brand collisions ("apple" the fruit), and feedback loops where a bad rewrite reduces clicks, which then looks like low demand. Always log the original and rewritten query so relevance engineers can debug.

How real systems do it

Large e-commerce engines typically keep a curated dictionary of brands, categories and attributes built from the catalogue itself, run fast taggers against it, and fall back to learned models for ambiguous tokens. Results for the most frequent few hundred thousand queries are precomputed and cached, so heavy models only run on the long tail. Every rewrite is logged next to the original query for offline analysis.

Check yourself Question 1 of 3
Discounted synonyms recall
From: Under the Hood: Machine Learning at the Query Boundary

The pipeline expands hoodie to sweatshirt. Why is the expanded term given a lower weight, such as 0.5, than the original term?

Key Takeaways

Query understanding bridges the semantic gap between imprecise user searches and structured index schemas.
Sub-10ms NER models extract structured attributes (brands, sizes, categories) to build filtered queries.
Query relaxation and synonym expansions prevent zero-result dead ends.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue