The word-to-documents list behind every search box: how it is built, stored, compressed and queried.
An inverted index flips the usual picture. Instead of "document โ words", it stores "word โ list of documents that contain it". To answer a search, you read the lists of the query words and combine them. You never open the documents.
The page "Full-Text Search Engines" explains how text is cleaned and ranked. This page is about the data structure under it: how the lists are laid out, combined, squeezed and spread over machines.
A normal (forward) index is like a book's pages: page 5 contains these words. An inverted index is like the index at the back of the book: the word "apple" appears on pages 3, 17 and 42. Looking up a word is fast because the dictionary of terms is sorted (or hashed), and each term points straight to its list.
A posting is usually more than a doc ID. It can carry the term frequency (how often the word appears, used for ranking) and the positions of each occurrence (used for phrase queries such as "red apple", where the words must sit next to each other).
| Index level | Each posting holds | What it allows | Cost |
|---|---|---|---|
| Doc IDs only | doc ID | Yes/no matching, AND / OR / NOT | Smallest |
| + term frequency | doc ID, count | Ranking by TF-IDF or BM25 | A little more |
| + positions | doc ID, count, positions | Phrase and nearby-word queries | Largest, often 2 to 3 times more |
| + payloads | extra bytes per posting | Field weights, boosts, offsets for highlighting | Varies |
| Query | How the lists are combined | Cost |
|---|---|---|
| A AND B | Intersect: advance the smaller pointer, keep equal IDs | About the size of the lists, less with skips |
| A OR B | Merge: output the smaller, keep equal IDs once | Sum of the list sizes |
| A AND NOT B | Walk A, drop IDs that also appear in B | Sum of the list sizes |
| Phrase โA Bโ | Intersect, then check positions differ by 1 | Intersect plus position checks |
| Many ANDs | Start with the shortest list, then intersect the rest into it | Much smaller than the order written |
With only 6 documents the saving is small, so look at the shape instead: the work depends on the list lengths, not on the number of documents. Real engines also use skip pointers to jump over long runs.
function intersect(a, b):
i = 0; j = 0; out = []
while i < a.length and j < b.length:
if a[i] == b[j]: out.push(a[i]); i++; j++
else if a[i] < b[j]: i++
else: j++
return out| Term | Meaning in easy words |
|---|---|
| Inverted index | A map from each term to the documents that contain it |
| Term | One cleaned word that is stored in the dictionary |
| Dictionary (lexicon) | The sorted list of all terms, with pointers to their postings |
| Postings list | The sorted doc IDs for one term |
| Posting | One entry: doc ID plus maybe frequency and positions |
| Term frequency (TF) | How many times a term appears in a document |
| Document frequency (DF) | How many documents contain the term. Also the length of the list |
| Skip pointer | A shortcut in a list that lets an intersection jump ahead |
| Delta (gap) encoding | Storing the difference from the previous ID instead of the ID |
| Segment | An immutable piece of the index. New data makes new segments |
| Merge | Combining segments into a bigger one and dropping deleted documents |
| Forward index | The opposite direction: document to its words |
| Choice | We gain | We lose |
|---|---|---|
| Store positions | Phrase queries | Index size and write cost |
| Heavy compression | Less disk and memory | More CPU to decode |
| Skip pointers | Faster AND on long lists | A little extra space |
| Few big segments | Faster queries | More merge work |
| Shard by document | Easy balance and writes | Every query hits every shard |
| Shard by term | Fewer machines per query | Hot terms, hard rebalancing |