Inverted Index Fundamentals
The Search Problem
Imagine searching for "machine learning" across 100 million documents. Without an index, you'd scan every document sequentially - taking hours. With an inverted index, the same search completes in milliseconds.
What is an Inverted Index?
An inverted index maps each unique word to the documents containing it - "inverting" the normal document → words relationship to words → documents.
The diagram above shows the fundamental difference: instead of scanning all documents sequentially, an inverted index provides instant lookup of which documents contain specific terms.
Core Components
Every inverted index contains:
- Dictionary: All unique terms found in the collection
- Postings Lists: For each term, which documents contain it
- Metadata: Term frequency, positions, document stats
Term: "python"
├── Doc1: frequency=1, positions=[0]
├── Doc3: frequency=1, positions=[5]
└── Total documents: 2, Collection size: 3
Simple Implementation
Here's a basic inverted index in Python:
Basic Inverted Index Implementation
Try adding different documents and searching for various terms to see how inverted indexing works
Why Inverted Indexes Work
Time Complexity:
- Without index: O(n × m) where n = documents, m = average document length
- With index: roughly O(k + p) for a simple lookup, where k is the number of query terms and p is the postings examined or returned; intersections and ranking add work based on posting-list sizes
Space Efficiency:
- Stores each unique term once regardless of document collection size
- Posting lists only reference document IDs, not full content
Scalability:
- Term lookup can remain fast as the collection grows, but common terms create longer posting lists and more ranking work
- Can be distributed across multiple machines
Text Preprocessing
Production indexes include sophisticated preprocessing:
Text Preprocessing for Search
Experiment with different preprocessing techniques and see how they affect search quality
Production Considerations
Memory Usage:
- Large vocabularies require significant RAM
- Solutions: Disk-based indexes, compression, sharding
Update Strategy:
- Rebuilding entire index for new documents is expensive
- Solutions: Incremental updates, merge strategies
Query Types:
- Basic implementation only supports AND queries
- Extensions: OR queries, phrase search, fuzzy matching
Real-World Applications
- Web Search: Google indexes 50+ billion web pages
- E-commerce: Amazon searches millions of products instantly
- Code Search: GitHub finds functions across billions of lines of code
- Enterprise Search: Internal document retrieval systems
What's Next
This basic inverted index returns unranked results. The next critical piece is relevance scoring - determining which documents best match a query using algorithms like TF-IDF and BM25.
Key concepts to explore:
- TF-IDF scoring: Mathematical foundation of search relevance
- BM25 algorithm: Production-grade scoring with tunable parameters
- Distributed indexing: Scaling across multiple servers
- Real-time updates: Handling live data streams
Inverted indexes transform impossible linear searches into lightning-fast lookups that power the modern web.