Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 13/ 31
Storage, Caching & Search

Search & Inverted Indexing

Power full-text search, relevance ranking, and sub-50 ms autocomplete at scale.

IntermediateInverted Index · BM25 · Tries3 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
Inverted Index & BM25 Ranking

How search engines tokenize text, intersect posting lists, and rank relevance.

Intermediate
Typeahead Tries & Vector Semantic Search

Power instant search-as-you-type suggestions and AI meaning-based retrieval.

Intermediate

In plain words

Search engines build a list of every word and the documents that contain it, like a book index for millions of pages. That makes "find all items with this word" instant.

Think of it like…

The index at the back of a textbook: instead of reading all 800 pages for “sharding”, you jump straight to “S → sharding: pp. 42, 118, 304”.

Where you’ve seen it

When you type "red running shoes" on Amazon, an inverted index finds products with those words and ranks the best matches first.

Words you’ll hear

Inverted index
A map from each word to the list of documents that contain it.
Tokenization
Splitting text into searchable words.
Relevance ranking
Ordering results by how well they match (e.g. BM25).
Elasticsearch
A popular search engine built on inverted indexes.

Visual diagram

Loading interactive diagram
Loading interactive exercise

Theory & deep dive

Why SQL `LIKE "%shoes%"` fails at scale

A standard B-Tree index can find prefixes (shoes%), not substrings in the middle of a sentence (%shoes%). Running LIKE "%shoes%" on 50 million product descriptions scans every row sequentially and locks up your database.

An inverted index flips the map: instead of Document → Words, it stores Word → List of Documents. Looking up "running" AND "shoes" simply intersects two sorted ID lists in milliseconds.

Key takeaway: Never run LIKE "%term%" in production SQL - sync searchable text to an inverted index (Elasticsearch / OpenSearch).

How it works, step by step

Step 1 of 4
Analyzer Pipeline

1. Tokenize & normalize text

Lowercase text, strip punctuation/stop words, and apply stemming or lemmatization (“running” → “run”) consistently at index and query time.

Same analyzer on write and query ensures matches align
Key numbers at this step
Steps
Tokenize → Lower → Stem
Extras
Synonyms, N-grams

When to use it & trade-offs

What it is

Search engines build inverted indexes mapping normalized tokens to document IDs, enabling fast boolean filters, BM25 relevance scoring, and prefix typeahead.

Why it matters: SQL LIKE "%term%" forces full table scans that collapse under millions of rows; inverted indexes resolve queries in milliseconds.

When to reach for it

  • E-commerce search with facets, synonyms, typo tolerance, and relevance tuning
  • Typeahead search bars debounced (100–200 ms) with cached prefix results
  • Log search and observability dashboards across terabytes of events

When to avoid or be careful

  • Using LIKE "%query%" in PostgreSQL on large text tables
  • Treating Elasticsearch as your primary source of truth instead of syncing via CDC

Search & Autocomplete Approaches

Inverted Index (Elasticsearch / OpenSearch)
Best for: Product catalogs, logs, documents, faceted filters

Strengths: Fast full-text matching, aggregations, geo filters, typo fuzziness.

Watch out: Near-real-time (~1s refresh lag); not an ACID primary store.

Prefix Trie (In-Memory)
Best for: Search bar autocomplete (top 5–10 query suggestions)

Strengths: O(L) lookup where L is prefix length; trivial top-K caching per node.

Watch out: High RAM if storing millions of raw strings without compression/sharding.

Vector Search (HNSW / pgvector)
Best for: Semantic similarity, RAG, image/recommendation retrieval

Strengths: Matches intent even when exact keywords differ.

Watch out: Approximate nearest neighbors (ANN) use heavy RAM and harder filtered search.

Interview signals & numbers to know

When this topic comes up

  • full-text search
  • autocomplete / typeahead
  • faceted filters
  • ranking
  • typo tolerance

Benchmarks & rules of thumb

Typeahead latency budget -
<50 ms
Search index refresh lag -
~1 second
Client typeahead debounce -
100–200 ms
Query shards target size -
10–50 GB per shard

Common mistakes & follow-ups

Common interview mistakes

  • Firing an autocomplete API request on every raw keystroke without client debouncing + cancellation
  • Running deep pagination (OFFSET 50000) on distributed search shards instead of search_after cursors
  • Storing giant unindexed fields or high-cardinality dynamic mappings that explode cluster memory

Likely interviewer follow-ups

  • Why does deep pagination (page=1000) kill performance in distributed search clusters?
  • How do you design personalized or trending typeahead suggestions without rebuilding the whole Trie on every click?
  • How do you combine BM25 keyword search with vector semantic search (Reciprocal Rank Fusion)?
Interview tip: Always clarify that your primary DB owns the truth and search indexes update asynchronously via Outbox/CDC.

Knowledge check

Loading quiz

Put it into practice

Design full architectures with AI feedback

Apply Search & Inverted Indexing inside real High-Level Design prompts or review Q&A flashcards.

Open AI HLD Practice →SD Interview Q&A
Previous topic
Blob & Object Storage
Next topic
Data Warehousing & Lakes