The Inverted Index — The Data Structure That Makes Search Possible
One data structure is responsible for the fact that you can search billions of documents in milliseconds: the inverted index. It's a beautifully simple idea — instead of asking "what words are in this document?", precompute "what documents contain this word?" — and it's the engine inside every search engine from Google to Elasticsearch. This post builds the inverted index from scratch and shows why it, and not raw scanning, is how search actually works.
Post 1 established that you can’t scan documents at query time — you must precompute an index. This post is that index. The inverted index is the single most important structure in information retrieval, and understanding it demystifies how a search box answers so fast. It’s also the exact structure that classical keyword search in RAG systems relies on.
The inversion: from documents→words to words→documents
A normal (“forward”) index maps each document to the words it contains — the natural way data is stored. But that’s useless for search: to find documents containing “reinforcement,” you’d have to check every document’s word list, which is the scan post 1 ruled out.
The inverted index flips this. It maps each word to the list of documents that contain it:
"reinforcement" → [doc 3, doc 17, doc 42, ...]
"learning" → [doc 3, doc 9, doc 42, ...]
"index" → [doc 17, doc 88, ...]
Now finding documents with “reinforcement” is a single lookup — go to that term’s entry and read its list. That list of document IDs for a term is called a postings list, and the whole structure is just a dictionary of terms, each pointing to its postings list. The genius is that the expensive work (figuring out which documents contain each word) is done once at indexing time, so at query time you only do fast lookups. This inversion — precomputing word→documents instead of scanning documents→words — is the entire reason search is fast, and it’s why “inverted index” is the foundational structure of the field.
Answering real queries: intersecting postings lists
The inverted index shines on multi-term queries, which is what makes it more than a lookup table. For a query like “reinforcement learning,” you want documents containing both words:
- Look up the postings list for “reinforcement” → [3, 17, 42]
- Look up the postings list for “learning” → [3, 9, 42]
- Intersect them → [3, 42] — the documents containing both.
Boolean queries become set operations on postings lists: AND is intersection, OR is union, NOT is difference. Because postings lists are kept sorted by document ID, these intersections can be computed extremely fast by walking the lists in parallel (like merging sorted arrays), skipping ahead efficiently — so even queries over huge postings lists resolve in a flash. This is the mechanical core of keyword search: the index turns “find documents matching this query” into fast set operations on precomputed sorted lists.
The postings lists usually store more than just document IDs, because ranking (post 4) needs more information: - Term frequency — how many times the term appears in each document (a document mentioning “reinforcement” ten times is probably more about it than one mentioning it once). - Positions — where in the document the term appears, which enables phrase queries (“reinforcement learning” as an exact adjacent phrase, not just both words somewhere) and proximity scoring. These extras are what let the index support not just matching but good ranking — the postings list is the raw material the scoring functions consume.
Why this scales — and the engineering around it
The inverted index is what makes web-scale search feasible, and a few properties explain why:
- Query time depends on results, not corpus size. A query touches only the postings lists of its terms, so searching a billion-document corpus for a rare term is nearly as fast as searching a thousand — you read only the relevant lists, never the whole collection. This is the scaling property that raw scanning could never have.
- It compresses beautifully. Postings lists (sorted integers) compress extremely well (storing gaps between IDs rather than the IDs themselves), so even a massive index stays compact enough to keep the hot parts in memory — critical for speed.
- It distributes naturally. The index can be sharded across many machines (split by document range or by term), so web-scale engines spread the index over thousands of servers and query them in parallel, merging results. This is how systems like Elasticsearch and the big web engines scale horizontally.
Real search engines add substantial engineering on top — incremental updates (adding new documents without rebuilding), handling deletions, caching hot queries, and tiering the index — but all of it is in service of the one core structure. When you use Elasticsearch, Lucene, or any full-text search, an inverted index is doing the work underneath, and the “keyword” half of a hybrid RAG retriever is exactly this. The structure is 50+ years old and still the bedrock, because the inversion idea — precompute word→documents — is simply the right answer to the matching problem at scale.
The throughline: the inverted index makes search fast by inverting the natural mapping — instead of documents→words, it precomputes word→documents (each term pointing to a sorted postings list of document IDs), so the expensive work happens once at indexing time and query time is just fast lookups. Multi-term queries become set operations on postings lists (AND = intersection, OR = union), computed quickly by merging the sorted lists; the lists also store term frequencies and positions to feed ranking (post 4) and phrase queries. It scales to the web because query time depends on the results, not the corpus size, postings compress superbly, and the index shards across machines — which is why a 50-year-old structure still underlies every full-text engine and the keyword half of hybrid RAG retrieval.
Key takeaways
- The inverted index maps each word → the list of documents containing it (a postings list of document IDs), inverting the natural document→words mapping — so finding documents with a term is a single lookup, not a scan.
- The win is precomputation: the expensive “which documents contain each word” work is done once at indexing time, making query time just fast lookups — this inversion is the entire reason search is fast.
- Multi-term queries become set operations on postings lists (AND = intersection, OR = union, NOT = difference), computed quickly because postings are kept sorted (merge-walk the lists) — the mechanical core of keyword search.
- Postings lists also store term frequency (for ranking, post 4) and positions (for phrase and proximity queries) — the raw material scoring functions consume.
- It scales to the web because query time depends on the results, not corpus size (you read only the query’s postings lists), postings compress extremely well (gap encoding), and the index shards across machines — which is why it underlies Lucene/Elasticsearch/web engines and the keyword half of hybrid RAG retrieval.
Further reading
- Inverted index — the word→documents structure behind search
- Search engine indexing — how indexes are built and maintained