19. Search and Inverted Indexes
The picture to keep in your head
Every question below is answered twice: first in the library, so you can picture it, then in the system, so you know exactly how it works. Each answer ends with a memory code to lock it in.
The picture to keep in your head
An old library has a wooden cabinet of subject cards. Each card is one subject, like "Cooking", and lists every book that covers it, with shelf numbers. You want books on cooking? You don't walk the shelves opening every book. You pull one card.
The librarian keeps it useful in a few ways. Everyone files under the same spelling ("Cooking", not "cooking", "COOKING" or "cookery"). If you want "cooking and Kerala", you lay two cards side by side and pick the books on both. The librarian hands you the best books first, not just any book that mentions the word. And when new books arrive, someone updates the cards, or the cabinet slowly lies to you.
That's an inverted index: instead of "book → its words", keep "word → the books it appears in".
Our running example: the bookstore's catalogue search. The catalogue has about 2 million titles (an assumption for practice), each with a title and a description, and customers type things like "database internals" or "kerala recipes" and expect good results in a fraction of a second.
| In the library | In the system |
|---|---|
| Opening every book on every shelf | LIKE '%word%', a full table scan |
| Everyone filing under the same spelling | The analyzer: lowercase, split, stop words, stemming |
| A card per subject listing the books | The inverted index: term → document IDs |
| Laying two cards side by side | Intersecting posting lists (AND queries) |
| The librarian's "best ones first" | Ranking: TF-IDF, BM25 |
| The drawer of cards starting "Coo..." | Prefix search and tries (autocomplete) |
| Updating cards when books arrive | Keeping the index in sync with the database |
| Several cabinets, one per wing | Sharding a search index |
Figure: keep a card per word that lists where it appears.