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 pointApproachWhy
GroupingReusable (termId, tokenIndex) integer sortEach term’s postings are written contiguously
EncodingOne varint stream per term: [docDelta, freq, posDelta*freq]Matches the Lucene104 format
Segment namesBase 36Java’s rule; otherwise multi-segment naming collides
Deleted bitmap.liv = header + bits2words(maxDoc) little-endian longs1 = live; padding bits past maxDoc must be cleared
MergePairwise merge tree (not whole-index reread)Bounded memory, output still equivalent to a single pass
AppendPre-register source FieldInfosPreserves field numbers; otherwise they all shift

2. Acceptance: byte identity

Merge and append correctness has a one-line criterion:

The forceMerge(1) / append-then-merge index must be byte-identical to a write done from scratch in one pass.

Measured: at 1M docs, forceMerge(1) produced 950,224,932 bytes, byte-identical to a single-pass write; a 250K append (150K + 100K) merged to 243,181,903 bytes, also identical, with exactly the same document and posting counts.

This standard is far stricter than “searches return the same results” — it forces every intermediate state to be correct.

3. Memory: 7GB → 398MB

The first version kept the whole index in memory before writing: 7.17GB peak at 1M docs — unusable.

After moving to RAM buffer + multiple segments + a merge tree:

MetricBeforeAfterJava
Peak RSS (1M)7.17GB398MB936MB (heap)
Index size (1M)1,299MB971MB917MB
Indexing speed14,976 docs/s35,361 docs/s33,232 docs/s

Later work reached 124K docs/s, 278.9MB and 123MB RSS at the 250K scale.

4. Writer traps

TrapSymptomRoot cause
Docs out of order after mergedocs out of order (94726 <= 99991)Merged doc ids must be compacted and renumbered
Java NPE on merged outputcollectionStats is nulldocCountWithTerms not maintained (index-wide stats)
Deleted count offbits.deleted=968 info.delcount=1000.liv padding bits set
_0.liv missingFile not foundGeneration 0 has no suffix; getNextDelGen() starts at 1
Segment name collisionCounter not max+1Must parse names as base 36
Append invisibleOnly 150K docscommit() returned early for flush-only runs
Merge loses field values7 sparse values missingassign instead of resize on carry-over

5. Coverage: 59.3% → 100%

Using luceneutil’s real wikimedium.1M.tasks, the port initially supported 6,664 / 11,236 = 59.3%. The missing part was a long tail:

Missing familyTasks (approx)How it was added
Wildcard500Automaton / bitset
Respell500DirectSpellChecker port
Fuzzy1 / Fuzzy2500 eachEdit-distance trie enumeration + TopTermsBlendedFreq rewrite
SpanNear (ordered/unordered)~1,036Spans machinery
IntervalsOrdered~536Interval iterators ported line by line
IntNRQ (numeric ranges)500points/BKD reader + writer + range query

After that: 11,236 / 11,236 = 100%.

6. Two methodologies

  1. A fixture is a claim: it claims “this code path exists”, but it may only cover the first block or the first segment. The rule: grow fixtures until the boundaries appear (multi-block, multi-leaf, multi-segment, with deletes), and make the gate fail rather than silently skip when a fixture stops exercising code.
  2. Lock invariants with scripts: enum/table alignment, .liv padding bits, field numbers being meaningful only within one index — each one gets an assertion or a check script.

7. Summary

  • Byte identity is the best acceptance test for a writer;
  • Memory problems are solved with buffer + segments + merge tree, not with a bigger heap;
  • Coverage comes from a gap list and item-by-item closure, not from “it’s about right”;
  • Grow fixtures until boundaries appear; make verification prove there are no holes.

Together, the four posts describe the whole method: define correctness with external oracles, prove it by reconciliation, and keep yourself honest with measurement.

Get the next essay

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