What Is an Inverted Index?
Understand the lookup structure that lets search engines find matching documents without reading every page.
Key takeaways
- An inverted index maps each term to a list of document identifiers, so lookups depend on the matching entries and not on reading everything.
- Text is normalized before indexing and at query time, and the two steps must agree or matches are silently lost.
- Storing word positions makes exact phrase search possible, at the cost of a larger index.
- Scoring methods such as TF-IDF and BM25 use counts held in the index to put the most relevant matches first.
An inverted index is a lookup table that maps each word to the list of documents containing it. Instead of reading every document when you search, the system jumps straight to your words and reads off where they occur. It's the core structure behind web search, email search, code search and most search boxes inside apps.
The name comes from flipping the natural arrangement. Documents are usually stored as "document, then its words". The inverted version stores "word, then its documents".
The problem it solves
Say you have three short documents and want every one that mentions "cable". The simplest method is to open each and scan it. That's fine for three. For millions, scanning on every query is hopeless.
The alternative is to do the reading once, ahead of time, and record the answers. When a query arrives, you consult the record. This trade is the heart of search: spend effort and storage during indexing so that queries stay fast.
Building one by hand
Take these tiny documents:
Doc 1: The router drops the cable signal.
Doc 2: Replace the cable before the router.
Doc 3: A fiber signal needs no copper cable. Split each into words, lowercase them, remove punctuation and note which document each came from. After grouping, part of the index looks like this:
| Term | Documents |
|---|---|
| cable | 1, 2, 3 |
| fiber | 3 |
| router | 1, 2 |
| signal | 1, 3 |
The list of terms is called the dictionary or vocabulary. The list of documents beside each term is its postings list. To find documents containing both "router" and "signal", fetch both lists and keep what they share: only Doc 1.
Tokenizing and normalizing
Tokenization means cutting text into units called tokens. It sounds trivial until you meet real writing. Is "Wi-Fi" one token or two? What about "C++", an email address or 192.0.2.10? Languages such as Chinese and Japanese don't put spaces between words, so they need dictionaries or overlapping character groups.
Normalization then makes different surface forms match. Common steps include:
- Lowercasing, so "Router" and "router" are the same.
- Removing or folding accents where users are unlikely to type them.
- Stemming, which chops endings by rule so "connected" and "connecting" share a root.
- Lemmatization, which uses vocabulary knowledge to map "better" or "ran" to base forms.
- Synonym expansion, so "TV" can find "television".
Each step is a trade. Aggressive stemming finds more matches and also more wrong ones. The key rule is consistency: the same processing must apply to documents and queries. If the index stores stemmed terms but the query isn't stemmed, matches vanish without any error message.
Very common words such as "the" and "of", called stop words, were often dropped to save space. Many modern systems keep them, because phrases like "to be or not to be" consist almost entirely of them.
What goes in a postings list
A bare list of document numbers answers yes-or-no questions. Real indexes usually store more with each entry:
- Term frequency, how many times the word appears in that document.
- Positions, where each occurrence falls.
- Field information, such as whether it appeared in the title or the body.
Positions enable phrase search. To match "cable signal" exactly, the engine finds documents containing both words, then checks that "signal" appears at the position right after "cable". Positions also support proximity ranking, where words appearing close together count for more.
The cost is size. Positional indexes can be several times larger than indexes holding only document numbers.
Answering queries quickly
Postings lists are kept sorted by document number. That makes combining them cheap. For an AND query, the system walks two lists side by side, advancing whichever has the smaller number, and records the numbers that match. It never needs to compare every entry against every other.
Two refinements help further. Starting with the rarest term keeps the intermediate set small. Skip pointers, which are periodic shortcuts inside a long list, let the system leap past stretches that can't match.
Compression and scale
Because postings are sorted, an index can store the gaps between document numbers instead of the numbers themselves. The list 1000, 1003, 1010 becomes 1000, 3, 7. Small numbers pack into fewer bytes, and reading less data from memory or disk often makes queries faster even after the cost of decompressing.
A very large collection is split across many machines. With document partitioning, each machine, or shard, indexes a subset of the documents. A query goes to all of them in parallel and the results are merged. Copies of each shard, called replicas, share the load and cover failures.
Ranking with index statistics
Finding matches is half the job; ordering them is the rest. The index already holds the numbers that basic scoring needs.
TF-IDF multiplies term frequency, how often the word appears in a document, by inverse document frequency, a measure of how rare the word is across the collection. A rare word that appears several times in one page says more about that page than "the" does.
BM25 is a widely used refinement. Extra repetitions add progressively less, and long documents are adjusted so they don't win merely by containing more words. It remains a strong baseline in many search systems.
Web engines go much further, blending these scores with link analysis, freshness and machine-learned models, as described in how ranking signals decide what you see.
Keeping the index current
Rewriting a huge sorted structure for every new document would be painfully slow. Many search libraries instead write new documents into small, unchanging segments, search all segments together and merge them in the background. Deleted documents are marked and physically removed during a later merge.
This explains a familiar delay. A newly saved item may take a moment to appear in an app's search, because it isn't searchable until its segment has been refreshed.
Limits, and what complements it
An inverted index matches terms. It struggles when a person and a document describe the same idea with different words: "my laptop won't charge" and "battery power adapter failure". Synonym lists help only so far.
Semantic search with embeddings addresses this by representing text as numeric vectors and finding nearby ones. Vectors are good at meaning and weaker at exact strings such as part numbers, error codes or names. Many systems therefore run both and merge the results, an approach called hybrid search. The same pairing supports retrieval-augmented generation, where retrieved passages are handed to a language model.
Fields, filters and facets
Real documents have structure, and an index can keep it. A product listing might have a title, a description, a brand, a price and a date. Search systems usually build separate postings for each text field, so a query can ask for "adapter" in the title only, or count a title match as worth more than a match in the description.
Filters work differently from word matching. A brand or category value is stored exactly as written, without tokenizing, so that "Acme Networks" stays one value. Numbers and dates are typically held in other structures designed for ranges, such as "published after a given day".
Facets are the counts you see beside a result list, showing how many matches fall under each brand or year. Computing them means going from matching documents back to their values, which is the opposite direction from the inverted index. Many systems therefore keep a second, column-style store of values per document for sorting and counting.
What a query looks like inside
Say a searcher types fiber router -rental. A simple engine turns that into a small plan:
- Normalize the words in the same way the documents were processed.
- Fetch the postings lists for "fiber", "router" and "rental".
- Intersect the first two, beginning with whichever list is shorter.
- Remove any document that appears in the third.
- Score the survivors and return the highest.
Real engines add shortcuts. If only the top ten results are needed, algorithms can skip documents whose best possible score couldn't reach the current tenth place. Frequently repeated queries and popular postings lists are also held in memory.
Where you already use one
Inverted indexes are all around you. Desktop file search builds one so it needn't open every file. Mail programs index messages, which is why searching your mail with operators is fast. Documentation sites often ship a compact index to the browser.
Open-source libraries such as Apache Lucene provide the structure, and servers such as Elasticsearch, OpenSearch and Apache Solr build on it. Several relational databases also include full-text index types that work on the same principle.
If your own search box returns odd results, inspect how it tokenizes. Feed it your hardest strings, such as hyphenated product names, version numbers and accented words, and see which terms come out. Most mysterious misses are explained right there.