Databases / Inverted indexes

Inverted indexes

How a search engine answers “which documents contain these words?” without reading the documents: a lookup table from each word to the places it occurs, built on four tiny texts.

Intermediate7 steps~6 min
Document — a piece of text with an id
Term — one cleaned-up word
Posting — the id of a document with that term
// THE SHORT VERSION

In 30 seconds

An inverted index is the data structure behind text search. Instead of storing, for each document, the words in it, it stores, for each word, the list of documents that contain it.

That list is the word’s posting list. A one-word search is a single lookup; a search for several words fetches their lists and keeps the documents they have in common.

If the lists also record where each word occurs, the engine can find exact phrases by checking that the words sit side by side.

Inverted indexes, step by step

STEPS01 / 07

Simplified: real engines also compress posting lists, add skip pointers to speed up intersection, and store counts so that results can be ranked by relevance rather than merely found.

STEP 01 / 07 The slow way: read it all
THE MATH
scan 4 documents = 13 words checked
  1. Step 1: The slow way: read it all. To find which documents mention “dog”, the obvious method is to read every document from start to finish. For four short ones that means checking 13 words. The work grows with the total amount of text, so for millions of pages every search would take far too long.
  2. Step 2: Cut the text into terms. Indexing starts by breaking each document into words, dropping punctuation and lower-casing them so that “Brown” and “brown” count as the same thing. Each cleaned-up word is called a term, and its place in the document, counted from 1, is its position. Real search engines often also trim word endings at this stage.
  3. Step 3: Flip it: term to documents. A document lists the terms it contains. The inverted index turns that around: for each distinct term it lists the documents that contain it. That list of ids, kept in ascending order, is the term’s posting list. The four documents hold six distinct terms, so the index has six entries.
  4. Step 4: One word: one lookup. A search for “dog” no longer touches the documents. The engine finds the term in its sorted dictionary, which takes a handful of steps even among millions of terms, and reads off the posting list: documents 2, 3 and 4. The cost depends on the length of that one list, not on how much text exists.
  5. Step 5: Two words: intersect lists. For “brown dog” the engine fetches both posting lists and keeps the ids that appear in each. Because the lists are sorted, one pass is enough: compare the two front ids, keep a match, otherwise step past the smaller one. 1 is skipped, 2 and 3 match, then the brown list runs out and the walk stops.
  6. Step 6: Store positions too. Knowing that both words occur in a document does not say whether they stand next to each other. So each posting also records the positions at which the term occurs in that document. In D2 “brown” is word 2 and “dog” word 3; in D3 they are words 1 and 5. This makes the index larger, and makes phrase search possible.
  7. Step 7: Phrases: check positions. A phrase search for “brown dog” first intersects the lists as before, leaving D2 and D3. Then it compares positions: the phrase is present only where “dog” sits exactly one position after “brown”. That holds in D2, where they are words 2 and 3, and fails in D3, where four positions separate them. Only D2 is returned.
VALUES SO FAR
docs4input
words13input
terms····index
dog····postings
brown····postings
both····result
phrase····result

Spotted a mistake on this page?