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:

  • Understand the format: every byte in an index file has a meaning; reading the format is the fastest path to understanding search engines;
  • Understand scoring: BM25, phrase matching, floating-point order — one ULP can flip the ranking;
  • Understand performance: why is Lucene fast? What do impacts, WAND and minCompetitiveScore each solve?

2. Four hard problems

ProblemCore questionPost
FormatByte layout and checksums of segments, terms dict, postings, doc values, pointsByte-level compatibility
ScoringBM25 float order, phrase position semantics, -ffp-contract=offBit-exact scoring
PerformanceThree pruning layers and measurement discipline (128 → 592 QPS)From 128 to 592 QPS
WritingMemory, merge, append, byte-identity acceptanceThe writer and the long tail

3. Reading order

  1. Rewriting Lucene in C++ (Part 1): Byte-level compatibility
  2. (Part 2): Bit-exact scoring
  3. (Part 3): From 128 to 592 QPS
  4. (Part 4): The writer and the long tail

4. What you get

Not a production search engine — far from it. What you get is a reusable verification method:

  • Use Java’s CheckIndex and byte-for-byte dumps as the external referee;
  • Four-way reconciliation (C++ on C++ index / C++ on Java index / and back) to prove both sides;
  • Revert checks to prove every fix is real;
  • Measure performance by per-document cost, not absolute numbers.

5. Scale and measured numbers

MetricValue
C++ codebase2,465 files / ~348K lines
Index formatLucene 10.4 (Java CheckIndex passes)
Test corpus1M documents, Top-10 queries
QPS128 (naive) → 592 (optimized)
p99 latency37.9 ms → 18.3 ms
Java baseline644 QPS / 22.7 ms p99
Equivalencesame documents, bit-identical scores

The most counterintuitive result: C++ has better p99 than Java but still 8% lower QPS. JIT optimization during long runs offsets C++’s static advantage — which says access patterns matter more than language choice.

5. An honest expectation

This series shows the wins and the failures: the MAXSCORE port that was reverted, the phrase-matching semantic bug, the stray processes that biased a benchmark by 9%. Simply because a series that only shows successes teaches nothing.

Get the next essay

New essays on products, business and tech — plus what the experiment's numbers actually say.