Inverted Index
First, What Is an Index?
Before we understand what an inverted index is, let's understand what an index is.
An index is any data structure that lets you answer a question without scanning everything. A phone book is an index: sorted by last name, it lets you find "Smith" directly instead of reading every page. A database's job is answering queries, and an index is the shortcut between the question and the data — trade a little extra storage upfront to avoid a full scan at query time.
The Baseline: Searching Without an Index
Now imagine a search engine over raw documents — no index at all, just the documents themselves. Say we have N documents, each containing K words, and a user types the query "penguin". To find every document mentioning penguins, the engine must open each document and scan its words:
for each of N documents: # N documents
for each of K words: # K words per document
does this word == "penguin"?That's O(K × N) comparisons — the engine reads every word in the entire corpus to answer a single query. With 1 million documents averaging 1,000 words each, one search costs a billion comparisons. And the next query costs a billion more.
Why This Doesn't Scale
The problem isn't just the constant — it's how the cost grows:
- Every query pays full price. Searching is O(K × N) each time, regardless of how small the answer is. A query matching just 3 documents still reads all N documents.
- Cost grows with the corpus. Double the documents, and every single query doubles in cost.
- No work is reused. Query "penguin" today and "penguin" tomorrow, and the engine re-scans everything from scratch.
Storage is cheap and documents are only getting bigger — O(K × N) per query is a losing battle. So let's build an index and fix this.
What About a Regular (Forward) Index?
Following the phone book recipe, index the corpus by the key we already have: the document. For each document, store its sorted list of words — a catalog:
Forward index (keyed by document):
doc1 -> [penguin, swims, the]
doc2 -> [cat, naps, the]
doc3 -> [penguin, waddle]This works beautifully when you already know the document — "fetch me doc2" is a direct lookup. But our query is "penguin", and the query doesn't know which documents contain it — that's the answer we're looking for. So the engine must still visit every one of the N entries and check each word list for "penguin" (a binary search per document if the lists are sorted):
for each of N documents: # must visit every entry
binary-search its K-word list # O(log K) membership testThat's O(N log K) — an improvement over the raw O(K × N) scan, but the dominant cost is untouched: the query still touches every document in the corpus, every single time. Add more documents and every query gets slower, exactly as before. The lesson: an index only helps when it's keyed by the same thing you search on. The phone book works because people look names up by name; here users search by word while our index is organized by document. The key is inverted — so the index must be too.
The Inverted Index
An inverted index inverts the mapping: instead of document → words, it's word → documents. For every word, it lists the documents containing it.
Building one is a single upfront pass over the corpus — read every document once, and for each word, append the document ID to that word's posting list:
Documents: Inverted index (built once):
doc1: "the penguin swims" "penguin" -> [doc1, doc3]
doc2: "the cat naps" "cat" -> [doc2]
doc3: "penguins waddle" "swims" -> [doc1]
"naps" -> [doc2]
"waddle" -> [doc3]
...Answering the Same Query
Now the query "penguin" costs a dictionary lookup plus one list read: jump to the word "penguin" (O(log V) in a sorted term dictionary, or O(1) with a hash — V is vocabulary size), and the posting list [doc1, doc3] is the answer.
- Query cost: O(log V + M) — where M is the number of matching documents, typically a tiny fraction of N. The engine touches only the documents that actually match, never the other N − M.
- For our 1M-document corpus: instead of a billion comparisons, it's a dictionary hop and a read of however many doc IDs contain "penguin".
The flip is also why the index scales: query cost depends on the vocabulary and the match count, not the total corpus size. Add ten times more documents and most queries still touch a handful of posting lists — cost grows with the answer, not with the database.
The Trade
The inverted index isn't free:
- Extra storage: the index itself (dictionary + posting lists) often rivals the corpus in size.
- Write cost: every new document must update the posting list of each of its unique words — writes go from "append a document" to "update many lists." This is why search engines historically batch updates, and why LSM-style engines pair posting lists with SSTables and compaction.
But the trade is the whole point: pay once at write time, and every read thereafter jumps straight to the answer.
Related reads
- S3 Data Fetching Techniques — how search engines fetch only the posting-list blocks they need, over object storage
- SSTables: The Sorted String Table — the immutable, sorted file format posting lists live in
- The Log Structured Merge Tree — how index updates get batched from memory into sorted disk files