Ask a search engine for "inverted index" and it answers in 5–50 milliseconds — across 10 million documents. Ask grep the same question over the same 10 million documents and you wait minutes, because grep reads every document and the search engine reads almost none of them. The difference is one data structure, invented before the web existed, and still the beating heart of every search box you use: the inverted index.
This is Season 3, Part 4. Season 1 covered B-trees and database indexes; this is the other index — the one that maps words to documents instead of keys to rows, and makes "find the needle" a lookup instead of a scan.
The ELI5 version
A normal index — the one in the back of a textbook — maps a topic to page numbers. "Inverted index: pages 42, 87, 130." A search engine's inverted index is the same idea, scaled up: every distinct word in the corpus maps to the list of documents containing it. That list is called a posting list, and each entry (a posting) records the document ID, how many times the word appears, and where.
A query is then not a scan but a set intersection: "inverted" AND "index" means walk the two posting lists together and keep the document IDs that appear in both. Both lists are sorted, so the merge is a single linear pass — and with the compression and skip tricks below, it is sub-millisecond even when the lists are millions of entries long. The work is proportional to the number of matching documents, never to the size of the corpus. That is the whole trick.
Anatomy: dictionary, postings, and three layers of clever
The term dictionary. Every distinct token in the corpus gets one entry. Lucene stores the dictionary as a finite-state transducer (FST) — a compressed automaton — so looking up a term costs O(|term|), proportional to the length of the word, regardless of whether the vocabulary has ten thousand terms or ten million. The dictionary is the only part that grows with vocabulary size; everything else grows with the corpus.
Posting lists and delta encoding. A posting list is a sorted sequence of document IDs. Storing raw 32-bit IDs wastes space, so Lucene stores gaps between consecutive IDs instead — small numbers compress well. The gaps are packed in blocks of 128 with Frame-of-Reference (FOR) encoding: take the largest gap in the block, use exactly that many bits for every gap in the block, and let a few oversized gaps be exceptions (PForDelta). The result is a few bits per document ID instead of 32. The notebook measures this on a toy corpus: raw IDs at 32 bits each vs delta gaps averaging 5.1 bits and FOR-packed blocks at 8.2 bits — a ~4x compression that also makes the lists faster to scan, because less data crosses the memory bus.
Skip lists. For an AND query, the naive merge walks both lists from the start. Skip lists store jump pointers every 128 documents, so the merge can leap past whole blocks that cannot match. Combined with galloping search (exponential probing into the longer list), the intersection of a rare term and a common term costs roughly the size of the shorter list, not the longer one. This is why "obscure-word AND the" is fast: the engine walks the rare term's short list and gallops through "the"'s enormous one.
Ranking: BM25, or why "the" doesn't win
Retrieval finds the candidates; ranking orders them. The industry default is BM25, and it rests on three intuitions:
- Term frequency saturates. A document that says "cache" 10 times is more relevant than one that says it once — but not 10x more. BM25's
k1parameter (default 1.2) bends the curve so the 2nd mention matters much more than the 20th. - Rare terms outweigh common ones. "Inverted" appearing in 100 documents is far more discriminating than "the" appearing in 9 million. Inverse document frequency (IDF) weights the rare term up.
- Short documents count more. A match in a 50-word note beats the same match buried in a 5,000-word page. The
bparameter (default 0.75) normalizes for document length.
The defaults — k1=1.2, b=0.75 — work surprisingly well out of the box, which is why almost nobody tunes them until relevance becomes a product problem. In 2026 BM25 has a second life as the sparse half of hybrid RAG: embeddings catch meaning, BM25 catches exact identifiers, error codes, part numbers, and rare tokens that embeddings blur. Production retrieval stacks run both and merge the results.
The write path: immutable segments
Lucene never updates an index in place. New documents buffer in memory and, at refresh (default every 1 second), flush as a new segment — a self-contained mini inverted index with its own dictionary, postings, stored fields, and doc values. Segments are immutable, which buys three things: no read locks (readers never block writers), aggressive caching (a segment's data never changes under the cache), and crash safety (a half-written segment is simply discarded).
The cost is merge debt. A busy index accumulates hundreds of small segments, and a background merge process continuously compacts them into larger ones — the same log-structured-merge idea as LSM trees, applied to search. Deletes are tombstones, not removals: a document is marked deleted and physically disappears only when its segment merges. The trade-off is visible in production as merge I/O competing with query latency, and it is the first thing to tune when p99 latency creeps up.
Why it breaks: four failure modes
Hot terms. A term in 90% of documents has a posting list nearly as long as the corpus. Every query touching it pays for that list. The fix is tiered: skip lists and Block-Max WAND let the engine prune blocks whose maximum possible score can't make the top-K, so it never fully decodes the hot list. If your p99 is dominated by one term, check whether it should be a stop word — or a filter, not a scored term.
Mapping explosions. In Elasticsearch, every new field name becomes a mapping entry, and a single bad client that sends user_48291 as a field name can create tens of thousands of fields — each with its own dictionary. The fix is strict mappings and index.mapping.total_fields.limit, set before the incident, not after.
Deep pagination. Page 1 of results is cheap; page 10,000 is not, because the engine must score and sort everything before it. search_after (a cursor) replaces offset pagination in every production system that survived its first incident.
Shard hotspots. Sharding splits the index by document, and queries fan out to every shard. If one shard holds the popular documents — a celebrity's posts, this week's news — it becomes the bottleneck for every query. Routing by a balanced key, or splitting hot content into its own index, is the fix; adding shards to a cold index is not.
The interview angle
"Design a search engine" is a system-design staple, and the inverted index is the answer's center of gravity. The strong answer starts here: dictionary (FST) + posting lists (delta + FOR, blocks of 128) + skip lists, then BM25 on top, then segments and merges for the write path, then sharding for scale. The follow-ups that separate candidates: what breaks when one term is in 90% of documents, how you paginate past page 1,000, and why you would add a vector index beside the inverted one instead of replacing it (hint: exact tokens vs meaning — the 2026 hybrid answer).
Takeaways
- An inverted index turns search from a scan into a lookup. Work scales with matches, not corpus size: 10M documents answer in 5–50 ms.
- Compression is performance, not just storage. Delta + FOR packing in blocks of 128 cuts posting lists to ~8 bits per doc ID — ~4x smaller and faster to scan.
- BM25's three intuitions — saturation (
k1=1.2), rarity (IDF), length normalization (b=0.75) — are the ranking defaults the whole industry runs on, and in 2026 they are the sparse half of hybrid RAG. - Segments are immutable; merges are the tax. No read locks and crash safety on the write path, merge I/O on the read path.
- The failure modes are operational, not algorithmic: hot terms, mapping explosions, deep pagination, shard hotspots — all four are fixed at index-design time, not query time.
Follow for the next part — Season 3, Part 5 is Bloom filters and probabilistic structures, and Part 6 takes on data lakes vs warehouses vs lakehouses. More on dev.to: @danielsamfdo.
Companion notebook: the runnable tutorial for this post — download it here (open in Colab/Jupyter).




