19. Search and Inverted Indexes
1
The picture to keep in your head2
19.1 Why doesn't `LIKE '%word%'` scale?3
19.2 How do you tokenise and normalise text?4
19.3 What is an inverted index?5
19.4 How does ranking work (TF-IDF, BM25 in brief)?6
19.5 How do prefix search and tries work?7
19.6 How do you keep the search index in sync with the database?8
19.7 Where does Elasticsearch fit in the big picture?9
19.8 How do you shard a search index?10
Memory card and where the analogy breaks11
Practice: five levels12
Interview questions13
Solutions14
Sources15
Code lab19. Search and Inverted Indexes
19.3 What is an inverted index?
3 min read+10 XP
In the library: The subject card cabinet. One card per subject, each listing every book that covers it. "Cooking and Kerala" means taking both cards and finding the books on both lists.
In the system: A normal (forward) view is document → words. An inverted index flips it: term → list of documents containing it. Each list is called a posting list.
Figure: finding a word becomes one lookup; AND becomes intersecting two lists.
How queries work:
- One word: look up the term, return its posting list.
- AND ("design systems"): look up both lists and intersect them. Start with the shortest list, the rarest word, and check the others against it; it's much less work.
- OR: union the lists.
- Phrases ("system design", in that order): the index also stores the position of each word in each document, so the engine can check that "system" is followed directly by "design". In the chapter's code, "designing systems" matches books 5 and 7, but the phrase "system design" matches only book 5.
What real engines add:
- Posting lists are kept sorted by document ID and compressed, so they're small and fast to intersect.
- The index is built in immutable segments: new documents go into a new small segment, and segments are merged in the background. Deletes are marked, then cleaned up on merge.
- Each posting also stores term frequency (how many times the word appears), needed for ranking.
Memory code: "Flip the book: a card per word, listing every page it's on."