Inverted Index: The Foundation of Full-Text Search
Fast recall
Quick revision
Core answer
Whether you understand the core data structure that makes search engines fast and can explain why it is fundamentally different from a SQL LIKE query.
Confusing an inverted index with a regular database index. A B-tree index on a text column speeds up exact prefix matches but cannot efficiently retrieve all documents containing a specific word in any position.
Explain term frequency (TF) and inverse document frequency (IDF) and how they combine in BM25 to score documents, rewarding rare terms that appear many times in a short document.
Interview-ready guide
Interview answer
An inverted index is the data structure that makes full-text search fast. Understanding how it works explains why match queries are O(1) lookups rather than O(n) scans.
Forward index vs inverted index
- A forward index maps document IDs to their contents —
doc:1 = ["spring", "boot", "java"]— which means finding all documents containing "boot" requires scanning every document's word list: O(n) - An inverted index flips this: it maps each unique term to the list of documents containing it —
"boot" -> [doc:1, doc:3, doc:7]— which means searching for "boot" is a single lookup in the term dictionary: O(1) to find the list, then O(k) to retrieve k matching documents
How Elasticsearch builds it
- When a document is indexed, its text fields pass through the analysis pipeline — the analyzer tokenizes the text into terms, applies filters (lowercase, stop-words, stemming), and stores each term with a posting list (the list of document IDs that contain the term)
to_tsvector('spring boot framework')in PostgreSQL and Elasticsearch's analyzer both produce the same conceptual result: a set of normalized tokens with their positions
Posting lists and relevance scoring
- The inverted index stores not just which documents contain a term, but how often the term appears in each document and the total frequency across the corpus — which means BM25 relevance scoring can be computed efficiently from the posting list data without re-reading documents
- BM25 (the default scoring algorithm) scores higher for: terms that appear frequently in the document, terms that appear rarely across the corpus (high IDF), shorter documents
Why full-text search can't use a B-tree
- A B-tree finds documents where a field equals an exact value or falls in a range —
WHERE title = 'spring boot'orWHERE price BETWEEN 10 AND 100; it can't find documents where a field contains a word - The inverted index is the only structure that maps terms to documents efficiently across the full corpus
Inverted Index: The Foundation of Full-Text Search
Direct Answer
An inverted index is a data structure that maps each unique term to the list of documents containing that term. It is the inverse of a document-centric forward index (document -> words). During indexing, text is analyzed into terms and recorded in the inverted index. At query time, the engine looks up the query terms in the index and retrieves the matching document IDs in microseconds, regardless of the total number of documents.
Code Example
// Example corpus (3 documents)
// Doc 1: "Spring Boot makes Java easy"
// Doc 2: "Java is fast and easy"
// Doc 3: "Spring makes apps easy"
// Inverted index built after analysis (lowercase, stop word removal):
{
"spring": { "postings": [{ "doc": 1, "freq": 1 }, { "doc": 3, "freq": 1 }] },
"boot": { "postings": [{ "doc": 1, "freq": 1 }] },
"java": { "postings": [{ "doc": 1, "freq": 1 }, { "doc": 2, "freq": 1 }] },
"easy": { "postings": [{ "doc": 1, "freq": 1 }, { "doc": 2, "freq": 1 }, { "doc": 3, "freq": 1 }] },
"fast": { "postings": [{ "doc": 2, "freq": 1 }] },
"apps": { "postings": [{ "doc": 3, "freq": 1 }] }
}
// Query: "spring java"
// Look up "spring" -> docs [1, 3]
// Look up "java" -> docs [1, 2]
// AND -> doc 1 (contains both terms) scores highest
// BM25 scoring factors:
// TF (term frequency): how many times the term appears in the document
// IDF (inverse document frequency): log(N / df) where N = total docs, df = docs with term
// "easy" appears in all 3 docs -> low IDF -> low importance
// "boot" appears in only 1 doc -> high IDF -> high importance for that query
// SQL LIKE comparison -- why it is slow
// SELECT * FROM articles WHERE body LIKE '%java%'
// Must scan EVERY row; no index structure helps for mid-string matches
Common Mistakes & Pitfalls
Confusing an inverted index with a regular database index. A B-tree index on a text column speeds up exact prefix matches but cannot efficiently retrieve all documents containing a specific word in any position.
Check your understanding
Answer these without looking back at the article.
- What is BM25 and how does it improve on TF-IDF for relevance scoring?
- How does Elasticsearch handle phrase queries using position information in the posting list?
- What is the role of the analyzer in building the inverted index?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.