Phrase Queries and Positional Indexes
What you'll learn: How search engines handle exact phrase matches like "machine learning" by storing word positions, not just their presence.
The Problem with Basic Inverted Indexes
So far, you've learned that an inverted index maps terms to documents. If a user searches for machine learning, a basic index can find documents containing both words—but it can't verify they appear together as a phrase. A document with "learning about machine tools" would incorrectly match.
Enter Positional Indexes
A positional index enhances each posting list by storing not just which documents contain a term, but where in each document. Instead of:
"machine" → [doc1, doc5, doc9]
You store:
"machine" → [doc1: [3, 45], doc5: [12], doc9: [7, 22]]
Where the numbers are word positions within each document.
Handling Phrase Queries
When processing a phrase query like "machine learning":
- Retrieve positional postings for both terms
- Find documents containing both words
- Check positions: For each matching doc, verify "learning" appears exactly one position after "machine"
- Return only documents where positions satisfy this adjacency constraint
Think of it like checking if two friends sit next to each other in a theater—you need their seat numbers, not just confirmation they're in the building.
Storage Trade-Off
Positional indexes are larger (roughly 2-4x the size of basic indexes) because they store position integers for every term occurrence. However, they enable precise phrase matching, critical for queries where word order matters: "New York" vs "York New."
Key Takeaway:
Positional indexes store word locations within documents, enabling exact phrase matching at the cost of increased storage—essential for high-quality search when term order and proximity matter.