What Is an Inverted Index? How Search Engines Find Words Fast
A map from each word to the list of documents that contain it, like the index at the back of a book. Search engines such as Elasticsearch use it to find words without reading every document.
Free PDF: the System Design cheat sheet, 18 pagesGet it free →
In short
An inverted index is a map from each word to the documents that contain it. It works like the index at the back of a book. To search for 'kafka log', the engine reads the list for 'kafka' and the list for 'log'. It keeps the documents on both lists, and never reads every document. Elasticsearch, Lucene and most full-text search engines are built on it. A forward index is the opposite direction: from each document to the words it contains.
In short
An inverted index is a map from each word to the documents that contain it. It works like the index at the back of a book. To search for 'kafka log', the engine reads the list for 'kafka' and the list for 'log'. It keeps the documents on both lists, and never reads every document. Elasticsearch, Lucene and most full-text search engines are built on it. A forward index is the opposite direction: from each document to the words it contains.
₹1,777 in India/$49 everywhere elseonce, for the whole course
The System Design course covers Inverted Index across a run of lessons, not one page. These 3 alone are about 75 minutes of step-by-step reading, every one with a quiz.
The problem: finding a word without reading everything
Imagine 10 million product descriptions in a database, and a user types 'red running shoes'. The simple way is to read every description and check whether it contains those words. That is a full scan, and it gets slower with every new product. A normal database index on the description column does not help, because it is sorted by the whole text, not by the words inside it.
Think of a thick textbook. To find every page about 'photosynthesis', you do not read the whole book. You turn to the index at the back, find the word in alphabetical order, and it tells you the page numbers. An inverted index is exactly that, for a computer: for every word, the list of documents (and sometimes the positions) where it appears.
How it is built and how a search uses it
Building it has two steps. First, text analysis: each document is broken into words, called tokens, and each token is made standard, for example lower case, so that 'Kafka' and 'kafka' become the same term. Elastic's documentation calls these steps tokenization and normalization, and the same rules are applied to the search query, so the words in the query match the words in the index. Second, for every term, the engine adds the document's id to that term's list. This list is called a posting list.
Searching reads the index instead of the documents. The terms are kept sorted, so the engine finds a term quickly. For an AND search, it takes the documents that are on every term's list (the intersection). For an OR search, it takes the documents on any of the lists (the union). Then it ranks the results, using how often each word appears and how rare it is across all documents.
import re
from collections import defaultdict
docs = {
1: "Kafka keeps messages in a log",
2: "Redis keeps data in memory",
3: "A log is read in order by Kafka consumers",
}
def tokens(text): # analysis: lower-case, split into words
return re.findall(r"[a-z0-9]+", text.lower())
index = defaultdict(set) # term -> posting list (ids of docs that contain it)
for doc_id, text in docs.items():
for term in tokens(text):
index[term].add(doc_id)
def search(query): # AND query: intersect the posting lists
lists = [index.get(t, set()) for t in tokens(query)]
return sorted(set.intersection(*lists)) if lists else []
print("keeps ->", sorted(index["keeps"]))
print("log ->", sorted(index["log"]))
print("kafka log ->", search("Kafka log"))
print("keeps memory ->", search("keeps memory"))
print("terms in the index:", len(index))
Words point to documents. A search reads a few short lists, not every document.
Document 3 writes 'Kafka' with a capital letter and is still found for 'kafka', because the same analysis (lower case, split into words) ran on the documents and on the query. 'keeps memory' finds only document 2, because only document 2 is on both lists.
Inverted index vs forward index
A forward index goes the other way: for each document, the list of words it contains. It is the natural shape of the data as it arrives, and it is useful when you already have a document and need its words, for example to show a snippet, to highlight matches, or to rebuild the inverted index. It is useless for finding which documents contain a word, because you would have to read every entry.
Search engines usually keep both. The inverted index answers 'which documents contain these words?', and a forward store holds each document's own fields to show the results. The inverted index lesson and the forward index lesson cover each in depth, with diagrams and exercises.
In Lucene and Elasticsearch, the index is a set of files that never change. New data makes new files; merges clean up.
Immutable segments. Lucene, the library inside Elasticsearch, writes the index as segments: files that are never changed after they are written. New documents go into new segments.
Merges. Many small segments make searches slower, so Lucene merges them into bigger ones in the background, by a merge policy.
Deletes are marked, not erased. Deleting a document records it in a deletion file. It is removed for good when its segment is merged. Updating a document is a delete plus a new add.
Positions for phrases. Storing the position of each word in each document lets the engine answer phrase searches, such as "running shoes" in that order.
Where it is used in production
Elasticsearch
Built on Lucene's inverted index, stored in immutable segments that are merged in the background.
Apache Lucene
The search library that defines segments, posting lists and the deletion file used by Elasticsearch, OpenSearch and Solr.
PostgreSQL
Offers full-text search with a GIN index, which is an inverted index over the words in a text column.
the question most readers have next
You know what Inverted Index means. What does it take to get it right in a real system?
A definition tells you what it is. Using it well means knowing where it fails and what to pair it with. The lessons below teach that step by step, starting with "Inverted Index".
It is like the index at the back of a book. For every word, it keeps a list of the documents where the word appears, and sometimes the positions. So a search reads a few short lists, not every document.
Why is it called inverted?
Because it turns the natural direction around. Documents naturally list their words (document to words, a forward index). The inverted index lists, for each word, its documents (word to documents).
What is the difference between a forward index and an inverted index?
A forward index maps each document to the words it contains; it is good for showing or highlighting one document. An inverted index maps each word to the documents that contain it; it is what makes word search fast. Search engines keep both.
Does Elasticsearch use an inverted index?
Yes. Elasticsearch stores text fields in Lucene's inverted index. Text is split into terms and normalized. Each term points to the documents that contain it. The index lives in segments that never change and are merged over time.
How does an inverted index handle a search with two words?
It looks up both words' posting lists. For an AND search, it keeps the documents on both lists (the intersection). For an OR search, it keeps the documents on either list (the union). Then it ranks them.
This page explains the idea. The "Inverted Index" lesson builds it up step by step, with diagrams. It is part of the System Design Masterclass course, which has 770 lessons. 18 of them are free to read. You pay once and keep it for life, with no subscription.
Get all 200 questions in the System Design and Distributed Systems pack
Each question has why it is asked, a full answer, what the interviewer listens for, the follow-ups, the common mistakes and a diagram (268 diagrams in all). A PDF you keep, junior to staff level.