Search & Inverted Indexing
Power full-text search, relevance ranking, and sub-50 ms autocomplete at scale.
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
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.
LIKE "%term%" in production SQL - sync searchable text to an inverted index (Elasticsearch / OpenSearch).How it works, step by step
Step 1 of 41. Tokenize & normalize text
Lowercase text, strip punctuation/stop words, and apply stemming or lemmatization (“running” → “run”) consistently at index and query time.
- 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
Strengths: Fast full-text matching, aggregations, geo filters, typo fuzziness.
Watch out: Near-real-time (~1s refresh lag); not an ACID primary store.
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.
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 ofsearch_aftercursors - 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)?
Knowledge check
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.