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.8 How do you shard a search index?
3 min read+10 XP
In the library: One cabinet can't hold cards for 2 million books, and one librarian can't answer every question. Split the books across three rooms, each with its own cabinet for its own books. A question goes to all three rooms; each sends back its best few; you pick the best overall.
In the system: Two ways to split an inverted index:
- By document (the common way): each shard holds all terms for a subset of documents. Every query goes to every shard (scatter-gather, Chapter 15); each returns its top 10; a coordinator merges them into the overall top 10. Elasticsearch works this way.
- By term: each shard holds some terms for all documents ("a" to "m" on shard 1). A one-word query hits one shard, but multi-word queries need several shards and huge posting lists for common words land on one machine. Rarely used in practice.
Figure: every shard searches its own books; the coordinator merges.
Practical points:
- Replicas of each shard spread the read load: more replicas, more queries per second.
- Shard count is hard to change later. In Elasticsearch, the number of primary shards is fixed when an index is created; changing it means splitting, shrinking or reindexing. Plan for growth, but don't create hundreds of tiny shards either; each one has overhead.
- Scores across shards: each shard computes IDF from its own documents, so scores can differ slightly between shards. With enough documents per shard it evens out.
- Deep pagination is expensive: page 1,000 of results means every shard returns its top 10,000 for merging. Limit page depth or use cursor-style "search after".
- Time-based indexes (one index per day, common for logs) make deleting old data cheap: drop the whole index.
Memory code: "Ask every room for its best, then pick the best of the best."