The Inverted Index: Terms to Tweets

A query for "earthquake worldcup" is not a scan — it's an intersection of two short lists, one per term.

The data structure

An inverted index maps each distinct term to a posting list: the IDs of every tweet containing that term, typically kept in time order so "most recent match" is just the head of the list.

term         posting list (tweet IDs, newest-first)
─────        ───────────────────────────────────────
"earthquake" → [ T₉₉₉, T₉₈₂, T₉₇₁, ... ]
"worldcup"   → [ T₉₉₅, T₉₉₀, T₉₈₈, ... ]
"coffee"     → [ T₉₉₈, T₉₉₃, ... ]

query "earthquake worldcup" → intersect posting lists → tweets containing both

Because both lists are already ID-sorted (and tweet IDs are Snowflake IDs — time-ordered, per main study eventual consistency), the intersection is a cheap merge-walk over two sorted sequences, not a hash-join or a full scan. This is the same "sortedness pays for itself" trick the timeline ZSET uses — here it's applied to a term's postings instead of a viewer's followed-author feed.

Getting from tweet text to postings: tokenization

Before a tweet can be indexed, it's broken into tokens — and Twitter's tokenizer has to make product-specific calls a generic text search engine wouldn't:

  • Hashtags and mentions are first-class tokens, not stripped punctuation — #worldcup and worldcup may be indexed as related-but-distinct terms, because a user searching #worldcup wants hashtag usage specifically.
  • Case folding and Unicode normalization — "WorldCup", "worldcup", and accented variants should generally converge to the same posting list, or search silently misses obvious matches.
  • Emoji and non-Latin scripts are tokens too — a large fraction of tweet volume is not English, and the tokenizer has to segment scripts (like CJK) that don't use whitespace the way English does.
  • No aggressive stemming — unlike classic document search, tweets are short and informal; over-aggressive stemming (running → run) can hurt as much as it helps for a corpus this colloquial, so tokenization tends to be lighter-touch than a traditional search engine's.
tweet text: "Coffee before the #WorldCup final ☕️🏆"
        │
        ▼ tokenize
[ "coffee", "before", "the", "#worldcup", "worldcup", "final", "☕️", "🏆" ]
        │
        ▼ append one posting to each term's list
"coffee" posting list  += T
"#worldcup" posting list += T
"worldcup" posting list += T
"final" posting list += T
"☕️" posting list += T
"🏆" posting list += T

Production trap: changing the tokenizer (adding stemming, changing hashtag handling, fixing a Unicode normalization bug) changes what terms map to what tweets going forward only — the existing index still reflects the old tokenization rules. A query run today for a term whose normalization just changed can silently return inconsistent results depending on whether a given tweet was indexed before or after the change, until the entire historical corpus is reindexed under the new rules. Tokenizer changes are effectively schema migrations across a corpus of hundreds of billions of documents — they get staged and rolled out deliberately, not shipped as a routine deploy.