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.

1. The format landscape

The target codec is Lucene104 (used by both Lucene 10.4 and 11.0.0-SNAPSHOT):

FileContent
segments_NSegment list, commit point, version
.siSegment metadata (Lucene99SegmentInfoFormat)
.fnmField infos (Lucene94)
.nvd / .nvmNorms
.fdt / .fdx / .fdmStored fields (LZ4)
.tmd / .tip / .tim / .psm / .doc / .pos / .payPostings (terms dict, postings, positions)
.dvd / .dvmDoc values
.kdd / .kdi / .kdmPoints (BKD tree)
.tvd / .tvx / .tvmTerm vectors

2. Three “common knowledge” facts that were wrong

Before starting, I wrote down an assumptions checklist from public material. Checking it against the source, half was wrong:

AssumptionReality
There’s a Lucene103PostingsFormatNo. codecs/lucene103/ only has blocktree; the live postings format is codecs/lucene104/Lucene104PostingsFormat
.tip is an FSTNo. Since 10.3 it’s a bespoke prefix trie (TrieBuilder/TrieReader)
Norms have compression modesNo — “norms have no compression modes”

And one trap you only learn the hard way: PerFieldPostingsFormat requires two field attributes in .fnm — PerFieldPostingsFormat.format = "Lucene104" and PerFieldPostingsFormat.suffix = "0". Without them, Java doesn’t error; it silently returns zero terms (terms(field) == null).

3. Endianness and checksums

  • Codec headers/footers and segments_N are big-endian; .si/.fnm/.nvm and payloads are little-endian;
  • VInt is 7-bit LSB-first; GroupVInt packs four 2-bit widths into one flag byte;
  • CodecUtil: CODEC_MAGIC 0x3fd76c17, FOOTER_MAGIC 0xc02893e8, a 16-byte footer = BE magic + BE algID(0) + BE CRC32.

One more detail: the writer must default segments_N to VERSION_86 (Lucene 10), otherwise released 10.4 throws IndexFormatTooNewException.

4. Verification: three oracles

  1. Java CheckIndex: accepts the C++-written index and checks its structure segment by segment;
  2. Byte-for-byte dumps: DumpIndex exports segments, terms, postings, stored fields and norms; compared with the Java side;
  3. Query reconciliation: 48 queries return identical documents and identical score bits on both indexes.

CompareIndexes.sh turns all three into one command that fails fast in either direction.

5. The long tail: doc values / points / term vectors

Byte compatibility isn’t done when postings can be read. Three long-tail formats each took a round:

  • Doc values (.dvm record layout): 6 types, with dense/sparse/table/gcd/zero-bit-width encoding branches;
  • Points/BKD: heap-numbered tree, DFS node stream, three leaf value encodings; doc ids have 7 encodings, and the bitset form’s header is two VInts (base word + word count), gated on absolute ids rather than 21/24/32-bit spans;
  • Term vectors: LZ4 chunks (4096 bytes, 128 docs per chunk), where “number of payloads” and “total payload bytes” are two different things.

6. Conclusion

Byte-level compatibility is tedious: endianness, versions, attributes, CRC — none can be wrong. But it buys something irreplaceable: an external referee. When Java says CheckIndex OK, the dumps are byte-identical, and 48 queries have identical score bits, “is it correct?” stops being a question you can talk yourself out of.

Next: scoring — why “close enough” is not enough, and why scores must match bit for bit.

Get the next essay

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