Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Topic 23 — Full-Text Search & Inverted Indexes

The third great index family after trees and hash tables: term → sorted posting list, plus the machinery that makes it fast (compressed blocks, FST dictionaries, BM25, block-max WAND) and the architecture that makes it writable (Lucene’s LSM-in-disguise segments). Home-turf bonus: RediSearch — what FalkorDB delegates full-text to — is being rewritten in Rust, and its inverted index crate is readable tonight.

Anatomy

  query "quick fox"
      │ analyzer: tokenize → lowercase → stem → stopwords
      ▼
  term dictionary                    posting lists (per term, doc-sorted)
  ┌──────────────┐   TermInfo       ┌────────┬────────┬────────┐
  │ FST: bytes → │ {doc_freq,       │block 0 │block 1 │block 2 │ 128 docs/block,
  │ term ordinal │  postings_range} │Δ-packed│Δ-packed│Δ-packed│ delta + bitpack
  └──────────────┘                  └────────┴────────┴────────┘
                                     skip data: per block
                                     {last_doc, max_score}  ← block-max WAND
  + fieldnorms (doc lengths, for BM25's B)
  + fast fields (columnar doc values — topic 12 inside a text index)
graph LR
    subgraph WP["write path — Lucene/tantivy segments = LSM"]
        W["docs"] --> MB["in-RAM segment<br/>(memtable)"] --> F["flush: immutable<br/>segment on disk"]
        F --> MP["merge policy<br/>(log-size tiers = compaction)"]
    end
    subgraph read path
        Q["query"] --> TD["FST term dict"] --> PL["postings + skips"] --> BMW["block-max WAND<br/>top-k"]
    end

Same shape as topic 4’s LSM: immutable segments, background merges, deletes as tombstone bitmaps, a reader that unions segments. Lucene discovered LSM independently because inverted indexes are cheap to build and expensive to update in place — exactly the LSM bet.

The two speed tricks

  1. Compression that keeps random access: doc ids stored as deltas, 128 per block, bit-packed to the block’s max width (tantivy postings/compression/mod.rs:3; RediSearch varint-encodes instead — redisearch_rs/varint). Blocks give you skip pointers for free.
  2. Score upper bounds: BM25 saturates at (K1+1)·idf, so every term and every block has a precomputable max score. WAND uses term maxima to find a pivot; block-max WAND (SIGIR’11) refines with per-block maxima and skips whole blocks that provably can’t beat the current top-k threshold.

Measured baselines (fts_bench, M3 Pro, 100K docs / 10M tokens / 7.9M postings)

laneresult
index buildgen 236 ms + build 335 ms (single thread)
oracle top-10, common∧common (272K postings)8.75 ms
oracle top-10, common∧rare (100K postings)6.34 ms
oracle top-10, rare∧rare (159 postings)8 µs
vec AND t0∧t1 (100K∧98K)97 µs
vec AND t0∧t5000 (100K∧172)52 µs — walks the dense list anyway

The 6.34 ms common∧rare lane is WAND’s whole reason to exist: the rare term’s idf ≈ 9 dominates, so almost none of the common term’s 100K postings can reach the top-10 — an exhaustive scorer touches them all anyway. The 52 µs dense∧sparse AND is roaring/galloping’s reason: two-pointer intersection is O(|dense|), not O(|sparse|).

Posting-list compression, measured (fts_bench, same index)

The “compression that keeps random access” trick is a claim about bytes, so the lane counts them. compress.rs encodes every posting list of the 100K-doc index two ways and checks both decode back to the original: varint (doc id as the delta from the previous one, 7 bits per byte — RediSearch’s shape) and 128-block bit-packing (tantivy’s shape: per block one width byte, the deltas minus one packed at the width of the block’s largest, then the term frequencies minus one the same way; the final partial block falls back to varint, as tantivy’s does). A bit width here is the number of bits needed for the largest value in the block — 32 - leading_zeros(max) — so it is set by the biggest gap in 128, not the typical one.

list class (by document frequency)listspostingsvarint B/postingbit-packed B/postingbit-packed vs raw 8 Bvs varint
head, df >= 10,000822,300,7712.0000.78610.2x2.55x
mid, 1,000 .. 9,9997892,021,2912.0601.1327.1x1.82x
low, 128 .. 9996,0371,813,7362.6101.6354.9x1.60x
tail, < 128 (varint in both)43,0921,732,3122.9392.9392.7x1.00x
all50,0007,868,1102.3631.5445.2x1.53x

Total: 62.9 MB raw (u32 doc + u32 tf), 18.6 MB varint, 12.2 MB bit-packed. The byte counts are exact and reproduce on any machine.

Read the head row with the arithmetic. A head list holds about 28,000 of the 100,000 documents (2,300,771 / 82), so the mean gap is about 100,000 / 28,000 = 3.6 and a typical gap needs 2 bits; but a block of 128 gaps has a largest gap of the order of 3.6 x ln 128 ~ 17, which needs 5 bits. Add the term-frequency block and the two width bytes (16 bits per 128 postings = 0.125 bits per posting) and the measured 0.786 B = 6.3 bits per posting is the right size. (An estimate: the lane does not print the block widths.) Varint cannot do better than one byte per value and every value here is under 128, so it spends exactly 2.000 B (one for the gap, one for the tf) on every head posting. The tail rows are identical because a list under 128 postings has no full block, and 43,092 of the 50,000 lists are that short: bit-packing buys nothing for most lists and 2.5x for the 82 head lists that carry 29% of the postings — a Zipfian vocabulary in one table.

The decode side is a negative result. A scalar decoder over the 82 head lists reads varint at 1.66 ns/posting and bit-packed at 2.96 ns/posting — the smaller encoding is slower to decode when no SIMD is involved, because unpacking an arbitrary width costs a shift-and-mask per value where varint on sub-128 values is a byte copy. tantivy’s bitpacking crate unpacks 128 values with SIMD at fixed widths; that is where the speed comes from, and this lane does not measure it. What the lane does establish is the size side of the trade and that its speed side is not free.

Reading guides

Experiments

filestatuswhat it shows
corpus.rsprovidedZipfian corpus (term id = rank), tokenizer
index.rsprovidedpostings + 128-block metas with per-block max BM25
bm25.rsprovidedK1/B, saturating tf, exhaustive term-at-a-time oracle
wand.rs wand_topkstubblock-max WAND: same top-k, fraction of the work
postings.rs Roaringstubarray/bitmap containers, AND/OR vs vec oracle
compress.rsprovidedvarint vs 128-block bit-packing: exact bytes per list class, scalar decode speed
bin/fts_bench.rsprovidedall lanes, stubs in catch_unwind

M23 checklist (capstone)

  • full-text index on node/edge string properties: analyzer + segment-per-milestone postings, BM25 top-k procedure
  • hybrid search: RRF fusion of BM25 top-k with M14’s HNSW top-k (score = Σ 1/(60 + rank_i))
  • posting lists ARE the graph trick: doc ids = node ids, so full-text hits feed directly into M20’s masked matrix ops