← Back

Inverted Index

5 min read

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:

text
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:

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:

text
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):

text
for each of N documents:          # must visit every entry
    binary-search its K-word list  # O(log K) membership test

That'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:

text
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.

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:

But the trade is the whole point: pay once at write time, and every read thereafter jumps straight to the answer.