← 1 Session 2 - The inverted index - Wed 02 Sep 3 →
All decks
  1. 1 Why retrieval nowMon 31 Aug
  2. 2 The inverted indexWed 02 Sep
  3. 3 Index construction and ranking foundationsMon 07 Sep
  4. 4 BM25: finish probabilistic rankingWed 09 Sep
  5. 5 Evaluation and the changing goals of retrieval benchmarksMon 14 Sep
  6. 6 Coarse retrieval and rerankingWed 16 Sep
  7. 7 LLM embeddings and rerankingMon 28 Sep
  8. 9 Hard queries: reasoning and ambiguityWed 07 Oct
  9. 10 Approximate nearest neighbour search and search systemsMon 12 Oct
  10. 12 Attention as retrievalMon 19 Oct
  11. 14 Memory as an architectural axisMon 26 Oct
  12. 16 Memory learned at test timeMon 02 Nov
  13. 18 Language models with an index attachedMon 09 Nov
  14. 20 Retrieval-augmented generationMon 16 Nov
  15. 22 Agentic retrieval and deep researchMon 23 Nov
  16. 23 Agent memory: write, manage, readWed 25 Nov
  17. 24 Learning what to rememberMon 30 Nov
  18. 26 Evaluating agent memoryMon 07 Dec
  19. 28 Privacy, personalisation and unlearningMon 14 Dec
Contents
  1. 1The inverted index
  2. 2Announcements
  3. 3Today
  4. 4Where we start
  5. 5What it is
  6. 6Where it came from
  7. 7What it is now
  8. 8The task
  9. 9The naive answer
  10. 10And it is the right answer
  11. 11Until it is not
  12. 12Write it down instead
  13. 13Now the query is arithmetic
  14. 14Then scale it
  15. 15Record only the ones
  16. 16Two halves, two lifetimes
  17. 17Building it is a transpose
  18. 18Why sorting is the transpose
  19. 19Our own postings are lopsided
  20. 20AND is one merge
  21. 21Shortest first
  22. 22Skip pointers
  23. 23And when they stop paying
  24. 24Heaps' law
  25. 25Zipf's law
  26. 26The queries users actually type
  27. 27What does the index return?
  28. 28Biword index
  29. 29And it does not hold
  30. 30A merge inside a merge
  31. 31The cost
  32. 32One thing we assumed
  33. 33Two structures
  34. 34Wildcards
  35. 35Both ends of the word
  36. 36General wildcards
  37. 37The k-gram index, and what it gets wrong
  38. 38Before you go

The recording

1

The inverted index

Information Retrieval for AI

Session 2 - Wed 02 Sep 2026

KAIST AI - Seong Joon Oh

2

Announcements

3

Today

  1. Why grep does not scale
  2. What the index is
  3. How postings merge
  4. Phrases
  5. Finding the term at all
  6. Building the index when it does not fit in memory

We stop where we stop. The rest opens next time.

4

Where we start

The inverted index: 1960s, and still what sits under Lucene, BM25, and the sparse half of a hybrid retriever.

We start here because the shapes it uses come back all term.

5

What it is

An inverted index maps each term to the list of documents that contain it.

retrieval  ->  d1, d3, d7, d9, ...
memory     ->  d2, d3, d9
hnsw       ->  d4, d11, d23
6

Where it came from

  • 1945 - Bush imagines the memex: “an enlarged intimate supplement to his memory”
  • c. 1950 - Mooers coins information retrieval
  • 1958 - IBM demonstrates auto-indexing machines, built on Luhn’s work

IIR §1.5, p. 17

7

What it is now

Lucene - the engine under Elasticsearch, OpenSearch and Solr.

BM25 over an inverted index is the sparse half of the hybrid search those systems ship natively.

As of 2026.

8

The task

Find every paper on the reading list that mentions

retrieval and memory but not reinforcement

9

The naive answer

No index. Scan every file, top to bottom, keep the ones that match.

$ grep -l retrieval *.pdf | xargs grep -l memory | ...
10

And it is the right answer

For a hundred papers, grep wins.

Do not build an index to search your own downloads folder.

11

Until it is not

  • It re-reads every document for every query.
  • retrieval but not reinforcement needs a second full pass, and a set difference held in your head.
12

Write it down instead

One row per term. One column per paper. One bit per cell.

d1d2d3d4
retrieval1011
memory0110
reinforcement0100
13

Now the query is arithmetic

d1d2d3d4
retrieval1011
memory0110
NOT reinforcement1011
AND0010

Two row fetches, one complement, one bitwise AND - and the answer is d3.

14

Then scale it

arXiv held 3,146,222 submissions on 26 Aug 2026, over a vocabulary of order 10M terms.

A paper holds about 2,000 distinct terms.

So one cell in five thousand is set. The matrix is a lie about where the information lives.

Distinct terms measured over the 97 papers in prep/papers/.

15

Record only the ones

For each term, keep the sorted list of documents where the bit is set.

That is the inverted index.

16

Two halves, two lifetimes

The dictionary is the list of terms. One entry per distinct term, holding its document frequency and a pointer.

The postings list is what the pointer reaches: the sorted docIDs for that one term.

dictionary                postings
-----------------------   -------------------------
retrieval  df=93   -----> d1, d3, d7, d9, ...
memory     df=67   -----> d2, d3, d9, ...
hnsw       df=5    -----> d4, d11, d23, d41, d55
LivesRead
Dictionaryin memoryonce per query term
Postingson diskstreamed
17

Building it is a transpose

Parsing gives you document order. The index needs term order.

emit    (caesar,d1) (brutus,d1) (caesar,d2) ...
sort    by term, then docID
collapse equal-key runs into postings lists
18

Why sorting is the transpose

Sort by term, and every pair for a term lands in one unbroken run - a pair with a different term is either smaller or larger, so nothing falls in between.

before   (2,d1) (1,d1) (2,d2) (4,d2)
after    (1,d1) (2,d1) (2,d2) (4,d2)
                |__ term 2 __|

So the collapse is one forward scan, no lookups - and a lookup per pair is the random access disk forbids.

19

Our own postings are lopsided

TermPapers
retrieval93 of 97
memory67
transformer44
bm2534
hnsw5

Nineteen-fold. The cost of an AND is set by the shorter list.

20

AND is one merge

Two pointers, both moving forward, never backward.

p:  1  4  7  9  13
q:  4  9  14
    ^  ^        ->  4, 9

Linear in the sum of their lengths, never in the size of the collection.

21

Shortest first

The intersection can never be longer than the smallest operand.

So process the shortest list first - and to know which that is before reading either, the dictionary stores document frequency next to the pointer.

retrieval  df=93  -> ...
hnsw       df=5   -> ...
22

Skip pointers

Pre-store jumps of about sqrt(length) so a long list can leap past documents the short list will not contain.

1  4  9 [skip->] 47  52  61 [skip->] 89 ...
23

And when they stop paying

Skips pay when the lists are lopsided and the index is static.

They were designed when stepping over a posting saved a 5 ms disk seek.

In memory it saves a few cache misses.

Seek time from IIR Table 4.1.

24

Heaps’ law

103104105106102103104105tokens read (T)distinct terms (M)our 97 papersM = 8.6 T^0.62RCV1: M = 44 T^0.49

Our 97 papers: 1.24M tokens, 52,074 distinct terms, and the curve is still climbing at the last token read.

Measured over prep/papers/, papers shuffled so the curve is not an artefact of alphabetical order. Book fit from IIR §5.1.

25

Zipf’s law

100101102103104100101102103104105ranktimes the term occursthe (62,713)work (1,420)worst-case (8)slope -1

24,654 of 52,074 terms occur exactly once - 47% of the vocabulary, one occurrence each.

That head is where idf comes from. That tail is where retrieval gets hard.

Measured over prep/papers/. Fitted exponent 1.18.

26

The queries users actually type

retrieval augmented generation

chain of thought

mixture of experts

27

What does the index return?

mixture AND experts returns every paper containing both words anywhere.

Papers
mixture31 of 97
experts34 of 97

The index we built cannot tell adjacency from co-occurrence.

28

Biword index

Make consecutive pairs the terms.

"mixture of experts"  ->  mixture-of, of-experts

Answers two-word phrases exactly. Longer phrases only by conjunction plus a post-check on the documents.

29

And it does not hold

  • The dictionary blows up - pairs, not terms.
  • retrieval augmented generation still comes back wrong.

So the general answer is to store positions inside the postings.

retrieval -> d1:[7, 112]  d3:[45]
30

A merge inside a merge

intersect documents        (as before)
  then for each document:
    intersect position lists
      under an offset constraint

p2 - p1 == 1 for adjacency.

31

The cost

One posting per occurrence, rather than one per document.

IndexPostings for a term
Non-positionalone per document containing it
Positionalone per occurrence, each with its offsets

The book’s rule of thumb: a positional index is two to four times the size of a non-positional index, depending on average document length.

IIR §2.4, p. 43.

32

One thing we assumed

The dictionary lookup is free.

It is not - and the structure you pick decides whether retriev* is answerable at all.

33

Two structures

LookupPrefix queriesOrderingGrowth
HashO(1)nonorehash
Search treeO(log M)yesyescheap
34

Wildcards

retriev*retrieval4,551retrieved730retrieve506retriever372retrieving218…34 more39 terms in our vocabulary. A hash table cannot walk them - it has no order.

Occurrence counts over the 97 papers in prep/papers/.

35

Both ends of the word

B-tree over terms → retriev*reretrireadrecallretrievalretrieverretrievesringrivalreverse B-tree over reversed terms → vallalavlelanoitarlaveirterlaveirterreveirterlevellexisintersect the two term setsreval → retrieval

A B-tree keeps terms in order, so retriev* is one range scan. A reverse B-tree does the same over reversed terms, so *val is a range scan too.

36

General wildcards

A k-gram index over character sequences.

A second index, over a different alphabet, that the system now has to build, store and keep consistent.

37

The k-gram index, and what it gets wrong

Write $ for a word boundary, so the term red is indexed as $red$ and its 3-grams are $re, red, ed$.

Then red* becomes the Boolean query $re AND red - starts with re, contains red.

red      ->  $re  red  ed$        matches
retired  ->  $re  ret  eti  tir  ire  red  ed$   also matches

retired contains both 3-grams and does not match the pattern.

So every candidate is checked against the original pattern before it is used.

IIR §3.2.2, p. 55.

38

Before you go

  • No reading due this week.
  • The recording goes up on the session page.

Questions