Inverted indexes and postings 00:00:01 Three signs, two query words I’m building a search system for a train and bike catalog, and I need to find documents with both blue and train. Right now, I’m indexing three items: one with a blue train, one with a blue bike, and one with a red train. I want to know which document has both blue and train. Without an index, I’d scan every document, but with one, I can just check the postings for each term. Without the index, the simple baseline inspects the three documents to discover which ones contain both words. The inverted index moves that discovery work to indexing time. The catalogue example is deliberately small enough to solve by eye. The point of the index is to avoid reopening every document when tomorrow’s catalogue contains millions. Build the mapping once, then follow just the requested terms. 00:00:50 Write the postings To keep things consistent, I normalize all text the same way during indexing and querying—lowercase, no punctuation, and split into single words. After normalization, blue maps to documents one and two, and train maps to one and three, with each ID in ascending order. Because the document IDs are sorted, I can merge the lists efficiently using a two-pointer technique, avoiding full scans. A posting can carry only a document identifier or additional evidence such as positions. Choose the evidence according to the questions the product actually needs to answer. Existence and adjacency are different promises. 00:01:31 Walk both pointers I start by comparing the first ID in each list—both are one—so I keep document one as a match. Now I advance both pointers: blue moves to two, train to three. Since two is smaller, I advance blue to the next ID. Advancing blue past identifier two exhausts the blue list. Train still points at three, but no blue identifier remains to match it. The intersection contains only document one. Whenever the smaller identifier trails the larger one, advancing the larger list could skip a possible match. Advancing only the smaller pointer either catches up or rules out that candidate permanently. 00:02:12 Words beside each other Just checking if both words appear doesn’t care about order or position—blue train and train blue both match if both terms exist. To find the exact phrase 'blue train', I store the position of each word so I can check if they appear adjacent in order. The phrase 'blue train' matches only if blue comes right before train. 'Train blue' doesn’t qualify, even though both words are present. Position data also has a cost. Removing common words without preserving their position gaps can change what counts as an adjacent phrase, so test the index and query analyzers as a pair. 00:02:50 Keep the index current Suppose document one is edited and no longer contains either word. The new published index should eventually omit identifier one from both old postings lists. Implementation details vary: an immutable-segment index can mark the old version obsolete and publish the replacement, then reclaim space during a later merge. Readers should not treat the old posting as a current match. I verify that only documents with both terms and correct phrase order are returned, ensuring the index stays accurate and usable. Replay the same blue-train query before and after changing a product. Check both the identifier intersection and the positional rule. A fresh document store can coexist briefly with an older published search index.