Writing a Search Engine from Scratch: Rewriting Lucene in C++

A search engine looks like “inverted index plus TF-IDF” — until you try to make it agree with Lucene bit for bit. This series records an attempt to rewrite Lucene 10.4 in C++. Not a toy: Java must be able to read a C++-written index (CheckIndex OK), the same queries must return the same documents and the same score bits, and performance must land in the same ballpark. 1. Why rewrite The point isn’t reinventing a wheel. It’s three things: ...

October 20, 2026 · 3 min · daviszhen

Rewriting Lucene in C++ (Part 4): The Writer and the Long Tail — From 59% to 100% Coverage

The first three posts covered reading, scoring and searching. The last one covers writing: the index writer, and a more prosaic question — how much do you actually support? 1. Writer architecture In one sentence: documents come in, get grouped by term → token, accumulate in a RAM buffer, and flush into a segment when full. Design point Approach Why Grouping Reusable (termId, tokenIndex) integer sort Each term’s postings are written contiguously Encoding One varint stream per term: [docDelta, freq, posDelta*freq] Matches the Lucene104 format Segment names Base 36 Java’s rule; otherwise multi-segment naming collides Deleted bitmap .liv = header + bits2words(maxDoc) little-endian longs 1 = live; padding bits past maxDoc must be cleared Merge Pairwise merge tree (not whole-index reread) Bounded memory, output still equivalent to a single pass Append Pre-register source FieldInfos Preserves field numbers; otherwise they all shift 2. Acceptance: byte identity Merge and append correctness has a one-line criterion: ...

October 20, 2026 · 4 min · daviszhen

Rewriting Lucene in C++ (Part 3): From 128 to 592 QPS — The Search Performance Fight

The first two posts were about “is it right.” This one is about “is it fast” — and not a 10% difference, but 5x. 1. Baseline: 128 vs 634 QPS 1M docs, luceneutil task set, Top-10: Scale C++ Java 250K docs 483 QPS 1,886 QPS 1M docs 128 QPS 634 QPS The gap isn’t constant — it grows with data size, which means the bottleneck is the per-document processing path, not startup overhead. ...

October 20, 2026 · 4 min · daviszhen

Rewriting Lucene in C++ (Part 2): Bit-Exact Scoring — The Floating-Point Traps of BM25

The previous post covered format compatibility: an index Java can read and verify. This one covers the harder part — scores must be bit-exact. Why so strict? Because verification is reconciliation: the same queries must return the same Top-N documents and the same score bits. If a score differs by one ULP (unit in the last place), documents can swap places at the boundary, the reconciliation never lines up, and you lose your only source of truth. ...

October 20, 2026 · 4 min · daviszhen

Rewriting Lucene in C++ (Part 1): Byte-Level Compatibility — When Java Reads a C++-Written Index

The easiest way to go wrong when porting Lucene to C++ is to say: “I’ll invent my own index format, and just be self-consistent.” That works, but it has no verification standard: you can’t prove your implementation is correct, only that it agrees with itself. I chose the other path: byte-level compatibility. The goal is that Java Lucene 10.4 can read a C++-written index (CheckIndex OK), C++ can read a Java-written index, and the dumps from both sides are byte-for-byte identical. ...

October 20, 2026 · 3 min · daviszhen

Six Real Traps in an Optimizer: Matching Results Don't Mean a Correct Plan

The previous two posts covered the port and the acceptance work. This one is only about traps — six real ones from the COLUMBIA + TPC-H project. What they have in common: everything looks fine until it isn’t. Trap 1: matching results, wrong plan The plan for ORDER BY ... LIMIT was once P_TOP at the root with QSORT below it — “truncate, then sort” — semantically wrong. It slipped past every automated check: at execution time DuckDB normalizes ORDER BY after LIMIT, so the results of this plan were byte-identical to the correct plan’s. ...

October 20, 2026 · 4 min · daviszhen

Porting a 1999 Cascades Optimizer to Modern C++: The COLUMBIA Story

If you want to hand-write a query optimizer, the cheapest starting point isn’t inventing one from scratch — it’s understanding a real Cascades implementation first, then getting it to run on your own machine. My sparring partner was COLUMBIA — the master’s thesis implementation by Yongwen Xu at Portland State University (1998–2000, advised by Shapiro and Maier), a textbook-grade Cascades/Volcano optimizer in 19,787 lines across 17 .cpp and 17 .h files. ...

October 20, 2026 · 4 min · daviszhen