Inverted Indexes: How Search Finds Documents Fast

Inverted IndexSearchRAG

One of the important ideas behind search engines is the inverted index. It makes finding documents that contain a word much more efficient.

The straightforward approach

Imagine storing the words found in each document:

doc1 → [golang, backend, docker]
doc2 → [python, ai, backend]
doc3 → [golang, kubernetes]

To find every document containing “golang” using only this representation, you would have to scan each document's words.

Flipping the relationship

An inverted index maps each term to the documents that contain it:

golang     → [doc1, doc3]
backend    → [doc1, doc2]
docker     → [doc1]
python     → [doc2]
ai         → [doc2]
kubernetes → [doc3]

These document lists are called posting lists. A search for “golang” now looks up its posting list.

Combining searches

Searching for “golang AND backend” becomes an intersection of two posting lists:

golang  → [doc1, doc3]
backend → [doc1, doc2]

Intersection → [doc1]

The index avoids scanning the full collection for every query. Efficient data structures do the work.

Where this shows up

Inverted indexes underpin keyword search in search engines, log analytics, document databases, and code search tools. They also support keyword retrieval within many RAG pipelines.

Sometimes a massive performance gain doesn't come from adding more compute. It comes from organizing data differently.