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
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. True or false? Explain in one line.
- A B-tree index on
titlemakesWHERE title LIKE '%kerala%'fast. - The same analyzer must be used for indexing and for queries.
- For "database internals", you should intersect starting from the longer posting list.
- In BM25, a word repeated ten times scores ten times as much as once.
- Elasticsearch should be the primary store for orders.
B. Which fix? Pick: analyzer change, synonyms, trie or edge n-grams, CDC, more replicas, field boost.
- Searching "DB" doesn't find "database" books.
- Books updated in the admin panel take hours to show new prices in search.
- Search is correct but slow at peak, with CPU maxed on every shard.
- A book with "databases" only in the description outranks one with it in the title.
- The search box needs suggestions after two letters.
Level 2: Calculate
LIKE '%word%'scans 2 million rows of about 1 KB, at 500 MB/s. How long per search? How many searches per second could one database do this way?- Each book has about 200 words after analysis. How many postings (word-in-book entries) does the index hold, at most?
- "kerala" appears in 50 books and "recipes" in 20,000. If you intersect by checking each item of one list against the other, which list do you walk, and how many checks is that?
- A search cluster has 3 primary shards, each with 2 replicas. How many shard copies exist in total? If one copy handles 200 queries per second, roughly what's the cluster's query capacity?
Level 3: Build
In Python:
analyze(text): lowercase, split on non-letters, drop stop words, a simple stemmer.- An
InvertedIndexwithadd(doc_id, text),remove(doc_id),search_and(query)andsearch_phrase(phrase)using positions. tfidf(query)andbm25(query); show that repeating a word gives TF-IDF a 3× score but BM25 much less.- A
Triewithinsert(word, popularity)andcomplete(prefix, k). apply_change((doc_id, version, text))that ignores out-of-order events and handles deletes.
Level 4: Debug
Customers say: "I searched Database and got nothing. But you sell 'Database Internals'!" The code:
python
- Find the bug and explain exactly why "Database" returns nothing.
- Fix it, including multi-word queries.
Level 5: Design
Design search for the bookstore: about 2 million books, titles and descriptions in English, customers expecting results in under 200 ms, filters by price and category, autocomplete, and admins updating prices and stock all day.
Deliver:
- Where search data comes from and how it stays in sync with the database.
- The analyzer and the ranking (which fields, what boosts, which business signals).
- How autocomplete works.
- How the index is sharded and replicated, with your reasoning.
- What happens when the search cluster is down.