Retrieval-Augmented Generation (RAG)

Inverted Index

Definition

An inverted index is the core data structure behind keyword search. Unlike a forward index (which maps documents to their terms), an inverted index maps terms to the documents that contain them. For each unique term in the collection, the index stores a posting list: the list of document IDs where the term appears, along with frequency information and optionally positional data. During search, the query terms are looked up in the index, and the matching posting lists are merged and scored using BM25 or TF-IDF. Inverted indices are extremely space-efficient and enable fast keyword searches across millions of documents.

Why It Matters

Inverted indices are the foundational technology behind all keyword search, from web search engines to Elasticsearch clusters to BM25 in RAG hybrid search. Understanding how inverted indices work is important for RAG practitioners because the sparse retrieval component of hybrid search relies on inverted index lookups. Inverted indices complement vector indices (which power dense retrieval) in hybrid RAG systems — each handles different query types optimally. When a user's query contains specific technical terms that must be matched exactly, the inverted index component reliably finds documents containing those terms.

How It Works

An inverted index is built by processing each document in the collection: tokenize the text into individual terms, apply normalization (lowercasing, stemming, stop word removal), and record each term-document occurrence in the posting list. At query time, each query term is looked up in the index, its posting list is retrieved, and documents are scored using BM25 scoring. Modern search engines like Elasticsearch and Opensearch manage inverted indices automatically, providing APIs for indexing documents and querying. In hybrid RAG systems, a separate Elasticsearch or Opensearch cluster typically handles the BM25/keyword search component, while the vector database handles semantic search.

Inverted Index Structure

Source Documents

doc-1: AI chatbot guide

doc-2: Retrieval methods

doc-3: Chatbot retrieval

doc-4: Embedding intro

doc-5: RAG with embeddings

Inverted Index

"chatbot"

doc-1doc-3doc-7

"retrieval"

doc-2doc-3doc-5

"embedding"

doc-4doc-5doc-6

Query "chatbot" → instantly retrieves doc-1, doc-3, doc-7 without scanning all documents

Real-World Example

A 99helpers customer integrates Elasticsearch as the sparse retrieval component of their hybrid RAG system. When a user's query contains specific product identifiers (model numbers, error codes, API endpoint names), the Elasticsearch inverted index locates documents containing those exact terms in milliseconds. The vector database semantic search handles the majority of queries where meaning matters more than exact terms. The hybrid fusion combines both result lists, and overall retrieval recall@5 reaches 93% — outperforming either component alone.

Common Mistakes

  • Building a custom inverted index when existing solutions like Elasticsearch or BM25 libraries exist — leverage battle-tested implementations rather than reinventing this infrastructure
  • Not maintaining the inverted index when documents are updated — stale index entries for deleted or modified documents produce incorrect search results
  • Skipping inverted index preprocessing (normalization, stemming) — without preprocessing, 'running' and 'runs' are treated as different terms, reducing retrieval coverage

Related Terms

Ready to build your AI chatbot?

Put these concepts into practice with 99helpers — no code required.

Start free trial →
What is Inverted Index? Inverted Index Definition & Guide | 99helpers | 99helpers.com