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.
1. BM25 must be reproduced with the same floating-point order
Lucene’s exact BM25 form is:
idf = log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) // computed in double, then narrowed
avgdl = sumTotalTermFreq / docCount // float
cache = 1 / (k1 * ((1 - b) + b * LENGTH_TABLE[i] / avgdl))
score = weight - weight / (1 + freq * normInverse) // weight = boost * idf
Three rules:
- Order matters. Algebraically simplifying
weight - weight/(1+freq*norm)intoweight*freq*norm/(1+freq*norm)is mathematically equivalent but not floating-point equivalent. - Compute idf in double, then narrow to float — converting early adds a rounding step.
-ffp-contract=offis mandatory. Compilers fuse multiply-add into FMA by default — more accurate, but different from Java. At 1M docs, this one flag cut differing lines from 875 to 762.
2. Phrase queries: position semantics are subtler than they look
Sloppy phrase has two easy-to-miss layers:
- Positions must be shifted by the term’s index within the phrase (
position - i), so slop counts position moves; - Match frequency is not a count but
1 / (1 + matchLength), accumulated per match span.
The first implementation only checked “span ≤ slop” and failed on a large fraction of phrase queries at 1M docs.
3. A bug that matched a phrase that wasn’t there
Reconciliation showed: LowPhrase: "the region" had C++ Top-1 = doc 454326, but grepping the corpus line 454327 found zero occurrences of “the region”, while the doc Java reported had exactly one.
So the document mapping wasn’t wrong — the C++ phrase matcher reported matches where the phrase didn’t exist. The root cause was the doc-alignment loop reusing postings cursors after loadPositions(), plus inconsistent advance(d) semantics when a term exhausted.
Using the corpus text as external ground truth settled it far faster than guessing across two codebases.
4. Vacuous verification: more dangerous than a bug
A classic reconciliation trap: diff A B | grep -c '^<'. If A is an empty file, grep returns 0 and the script reports “DIFFS=0” — green, but nothing was compared.
We hit this twice:
- Once the comparison command’s output went to the wrong place, leaving one side empty;
- Once the task kind was parsed wrongly (luceneutil writes
HighSpanNear, we parsedSpanNear), the tasks were silently skipped, and 0 results “all agreed”.
The fix is in the script: assert both artifacts are non-empty, never discard the compared command’s stderr, and require the task count to match.
5. Four-way reconciliation
The final reconciliation is “four-way”:
| Combination | Meaning |
|---|---|
| cpp-cpp | C++ queries the C++ index |
| cpp-java | C++ queries the Java index |
| java-cpp | Java queries the C++ index |
| java-java | Java queries the Java index |
All four must agree on documents and score bits. That way, neither a write bug nor a read bug can hide.
6. Summary
- Scores must be bit-exact, or ranking is unstable and reconciliation meaningless;
- BM25’s floating-point order must not be algebraically simplified;
-ffp-contract=offis required; - Phrase position shifting and frequency formula are high-frequency error points;
- Use the corpus text as ground truth;
- Beware vacuous verification: “0 results all agree” is the most dangerous green light.
Next: search performance — the fight from 128 QPS to 592 QPS.
Related
- Rewriting Lucene in C++ (Part 1): Byte-Level Compatibility — When Java Reads a C++-Written Index
- Rewriting Lucene in C++ (Part 3): From 128 to 592 QPS — The Search Performance Fight
- Implementing Git for Data on DuckDB (Part 3): Branches, diff, pick, merge — The Version Primitives of Data
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.