The inverted index — term to docs
An inverted index stores term → sorted list of doc ids (a posting list), so a word lookup is one hash-into-sorted-list and Boolean queries become merges of sorted iterators — the cost collapses from O(documents) to O(matches).
Asking 'which rows contain this term?' needs a precomputed map from term to rows. Build it once, query it many times.
Scene 02
The inverted index — term to docs
- Watch
- Try it
- Predict
- Capture
Five books, six terms each. Watch the term dictionary populate one term at a time, with each posting list showing exactly which doc ids contain that word. The walk ends on the scene-1 query 'distributed systems' — same query, two hash lookups, one merge.
Highlighted lines are the ones running in the diagram right now.
def tokenize(book):raw = split_on_whitespace(book.body)terms = [lowercase(t) for t in raw]return set(terms) # dedupe within a doc
def lookup(term):if term not in dictionary:return []return dictionary[term] # sorted list of doc ids
def and_merge(a, b):i, j, out = 0, 0, []while i < len(a) and j < len(b):if a[i] == b[j]: # match — emit and step bothout.append(a[i])i += 1; j += 1elif a[i] < b[j]:i += 1 # advance the smaller cursorelse:j += 1return out # bounded by min(|a|, |b|)
Where this sits in Build a distributed search engine (Elasticsearch / OpenSearch style)
Scene 02 of 12. Flip the relation: a precomputed map from each word to the doc IDs it appears in turns O(documents) Boolean queries into O(matches) sorted-list merges.
Up next. The map is beautiful when frozen and miserable when mutated — every insert touches the term dictionary at random terms, every delete shifts posting lists. The next scene buys back mutability without touching what is already on disk.
All 12 scenes in Build a distributed search engine (Elasticsearch / OpenSearch style) · Every curriculum