Skip to content
System Design Explorer
storage

Inverted Index

By Ankur NayakReviewed

An inverted index maps each word to the list of documents that contain it, so a search engine can answer a query by reading a few short lists instead of scanning every document. It is the core data structure behind web search and full-text search engines.

Inverted Index diagramComponents: Documents, Indexer, Inverted Index, Query Server, Searcher. Connections: Documents to Indexer; Indexer to Inverted Index (sort by word); Searcher to Query Server (query); Query Server to Inverted Index (postings per word).DocumentsObject storageIndexerBatch processingInverted IndexSearch indexQuery ServerServiceSearcherClient
Indexing and querying

How it works

A forward index lists, for each document, the words it contains. Inverting it gives, for each word, the list of documents (and often positions) where it appears: the postings list. A lexicon (dictionary) maps each word to where its postings list lives.

Building one is a sort: parse documents into (word, document, position) entries, then sort and group them by word. The original Google prototype (1998) worked exactly this way: the indexer turned pages into 'hits' (word, position, font size, capitalisation) and distributed them into barrels as a partially sorted forward index; a sorter then re-sorted the barrels by word ID to produce the inverted index, and a lexicon pointed into it.

Answering a query means fetching the postings list of each query word and intersecting them (for AND) or merging them (for OR), then ranking the matching documents with signals such as word positions, link-based scores like PageRank, and freshness.

At web scale the index is sharded across many machines and kept up to date continuously. Google's 2010 Caffeine index replaced a layered index whose main layer refreshed every couple of weeks with one updated continuously in small portions.

Key points

  • word → postings list of documents (and positions)
  • Build by sorting (word, document) pairs
  • AND = intersect lists, OR = merge lists
  • Rank matches with positions, links, freshness
  • Shard by document or by term at scale

When to use it, and pitfalls

Use it for

  • Full-text search over documents, products or messages
  • Log search and observability tools
  • Autocomplete and filtering by tags

Watch out for

  • Very common words create huge postings lists; skip or compress them
  • Index freshness: batch rebuilds go stale; plan incremental updates
  • Keeping the index consistent with the source of truth

Trade-offs

Shard by document vs. by term

Shard by document (each shard indexes a subset of documents) and query all shards in parallel.

Pros: Even load; adding documents touches one shard; Each shard answers queries independently

Cons: Every query fans out to every shard

Used in

Frequently asked questions

Why is it called an inverted index?

Because it inverts the natural document → words relationship. A forward index lists the words in each document; the inverted index lists, for each word, the documents that contain it, which is what a search needs.

How does a search engine use an inverted index to answer a query?

It looks up each query word's postings list, intersects the lists to find documents containing all the words (or merges them for any word), and ranks the matches using signals such as where the words appear and link-based scores like PageRank.

Sources