Skip to the content.

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?


⚙️ 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:

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”

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:


✂️ 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.)

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:

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:

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?


🔍 8. Searching uses the inverted index

To search "apple iphone":

Fast because the lookup is O(log T), T = number of terms.


🧠 Summary (Very Clean)

⭐ Indexing Steps

  1. Receive document
  2. Analyze text → tokenize, lowercase, filters
  3. Build vocabulary (terms)
  4. Create posting lists
  5. Write segment (Lucene)
  6. Merge segments
  7. Search using inverted index

⭐ Search is fast because

ElasticSearch does not scan documents. It does term → posting list → docIds lookup.


Just tell me!