Course contents
On this page
Find products containing two words
Build an inverted index from word membership, then check what it can and cannot prove.
The shop wants products whose descriptions contain both “red” and “bag.” An index ordered by product number helps find a known ID, but this request begins with words. Scanning every description would work; can we organize the data around the question instead?
For this lesson, a word match means exact membership in a set of lowercase words. We ignore stemming, accents, ranking, and word order until we have understood the basic lookup.
Reverse the direction of the reference
Start with three descriptions:
| Product | Words in its description |
|---|---|
| 12 | red, notebook |
| 27 | red, canvas, bag |
| 46 | blue, bag |
An inverted index maps each word to the IDs of products containing it. The list of matching IDs for one word is often called a posting list. We have reversed the relationship from product → words to word → products.
red
12 · 27
bag
27 · 46
Present in both
27
An intersection contains items shared by both sets. With sorted ID lists, an implementation can advance through them together: compare 12 with 27, discard 12, then match 27 with 27. It does not need to compare every possible pair.
For the exact word-membership question, the intersection identifies the answer, assuming the index is current. The engine still needs to obtain the requested output fields and apply any additional conditions, such as whether the product is available.
Ask a slightly different question
Now the request is the phrase “red bag,” with the words next to each other in that order. Our index says only that both words occur. It does not record their positions. “Red canvas bag” would incorrectly qualify if we treated membership as proof of the phrase.
The engine can store word positions, or use membership to produce candidates and then check the original text. This distinction appears in many specialized indexes: first narrow the candidates, then evaluate whatever the index does not establish.
Follow an update
Product 27’s description becomes “blue canvas bag.” Which posting lists change?
Update the relationship in both directions
Remove 27 from the red list and add it to the blue list. It remains in the bag and canvas lists. A current search must not see an inconsistent mixture of the old and new memberships. The engine must coordinate these changes with the row update using its chosen transaction rules.
The useful structure follows the query. Spatial bounds, column summaries, and approximate vector indexes answer other questions with different guarantees; their extended treatments remain linked below.
PostgreSQL’s text-search index documentation describes production choices, including cases that require rechecking candidates. Our example explains the word-to-record relationship those choices build on.
Extended reference
The extended chapter preserves the original technical coverage, additional examples, and citations. The explanation above supplies the concepts needed for this page; the reference develops the specialist cases.
Find a section from the original chapter
- Learning outcomes
- One query language can hide several different search problems
- Columnar access and analytical locality
- Inverted indexes and full-text search
- Time-series organization
- Spatial indexes and conservative candidates
- Vector similarity and approximation
- Combining access paths
- Maintenance and operational boundaries
- Candidate generation and exact checking form one contract
- A worked inverted-list intersection
- Ordering makes block summaries effective
- Time windows require mathematically valid summaries
- Spatial cells trade update churn against candidate count
- Recall must be evaluated after filters
- Building several indexes creates a coordination problem
- Case synthesis: query shape chooses the physical advantage
- Draw the candidate pipeline before naming an index
- Case interpretation and design conclusion
- Sources and further reading