An inverted index is a data structure used to make search fast—especially in search engines (Google, ElasticSearch, Solr), document retrieval systems, and full-text search.
✅ Simple Explanation
Think of an inverted index as a dictionary that maps each word → the list of documents where that word appears.
This is inverted because normally you store documents and the words inside them (document → words). But here you invert it to store words → documents.
📘 Example
Suppose you have 3 documents:
Doc1: "apple banana mango"
Doc2: "banana mango"
Doc3: "apple grapes"
👉 Without inverted index
To find documents containing “apple”, you would have to scan all documents → slow for large data.
👉 With inverted index
We build a mapping like this:
| Word | Documents |
|---|---|
| apple | Doc1, Doc3 |
| banana | Doc1, Doc2 |
| mango | Doc1, Doc2 |
| grapes | Doc3 |
Now if you search “apple”, you instantly get: Doc1, Doc3 No need to scan everything.
🧠 Why is it called inverted?
Because the usual format is:
Document → Words (“apple” is inside Doc1)
But this structure flips it to:
Words → Document List (“apple” → Doc1, Doc3)
📚 Where is it used?
- Search engines (Google, Bing)
- ElasticSearch, Apache Solr, Lucene
- Information retrieval systems
- Databases with full-text search support
⚙️ How does it work internally?
An inverted index contains:
1. Dictionary / Vocabulary
List of unique words found across all documents.
2. Postings List
For each word, a list of:
- document IDs
- positions of the word inside the document (optional)
- frequency (optional)
Example posting:
"apple": [
{docId: 1, positions: [0], freq: 1},
{docId: 3, positions: [0], freq: 1}
]
⭐ Why use it?
Because it makes search O(1) or O(log n)
Instead of scanning thousands of documents → O(n) time.
🚀 Bonus: How “AND” & “OR” search works?
Example: Search for “apple AND mango”
- apple → Doc1, Doc3
- mango → Doc1, Doc2
Intersection: → Doc1
For “apple OR mango”
Union: → Doc1, Doc2, Doc3
🏁 Summary
Inverted Index = A fast lookup table mapping each word to all documents containing it. It is the backbone of modern full-text searching.
ElasticSearch builds an inverted index automatically for every text field you store. It does this using several internal steps:
Below is the clear + simple + complete explanation.
🧱 1. Document comes in
You index a document:
{
"title": "Apple launches new iPhone"
}
ElasticSearch receives:
- index name
- document ID
- field values
✂️ 2. Text is analyzed (Tokenizer + Filters)
Before adding to the inverted index, ES runs the field through an Analyzer, which includes:
✔ Tokenizer
Splits text into terms (tokens). Example tokenization:
"Apple launches new iPhone" → ["apple", "launches", "new", "iphone"]
✔ Token Filters (lowercase, remove stopwords, stemming, etc.)
- Lowercase →
apple, launches, new, iphone - Stemming (optional) →
launchinstead oflaunches - Remove stopwords (optional) → remove words like “new”, “the”, “a”, etc.
So final processed terms might be:
["apple", "launch", "iphone"]
📚 3. Build the dictionary (unique words)
For every field, ES keeps a vocabulary (unique sorted terms).
Example vocabulary:
apple
iphone
launch
📋 4. Build Posting Lists
For each term, ES stores:
- docId
- term frequency (tf)
- positions (for phrase queries)
- offsets (for highlighting)
- norms (for scoring)
Example posting list for "apple":
"apple": [
{ docId: 101, freq: 1, positions: [0] }
]
📦 5. Store the inverted index in Apache Lucene segments
ElasticSearch does not build the index itself. It uses Apache Lucene, which stores everything in segments.
Segment contains:
- Inverted Index for all fields
- Store fields (original JSON)
- Doc values (for sorting/aggregations)
- Term dictionary
- Posting lists
A segment is immutable.
Once written, it never changes.
🔄 6. New documents create more segments
Each batch of documents creates a small new Lucene segment.
Example:
Segment_1 → 1000 docs
Segment_2 → 700 docs
Segment_3 → 1500 docs
Searching must check all segments → slow over time.
🧹 7. Segment Merging
ElasticSearch automatically merges smaller segments into a larger one:
Segment_1 + Segment_2 → NewSegment
Why?
- Removes deleted docs
- Optimizes posting lists
- Improves search speed
🔍 8. Searching uses the inverted index
To search "apple iphone":
- ES looks up
"apple"in the inverted index → list of docIds - Looks up
"iphone"→ list of docIds - Executes boolean logic (AND/OR/phrase/etc.)
- Scores using BM25
Fast because the lookup is O(log T), T = number of terms.
🧠 Summary (Very Clean)
⭐ Indexing Steps
- Receive document
- Analyze text → tokenize, lowercase, filters
- Build vocabulary (terms)
- Create posting lists
- Write segment (Lucene)
- Merge segments
- Search using inverted index
⭐ Search is fast because
ElasticSearch does not scan documents. It does term → posting list → docIds lookup.
Just tell me!