AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
XERJ On-Disk Footprint — Diagnosis + Merge-Level Compression Fix (2026-07-09)
TL;DR — On an identical 100k-doc representative corpus (keyword + text + numeric + date + bool), force-merged to one segment on both engines:
| Engine | On-disk (index/shard dir) | vs ES |
|---|---|---|
| XERJ before | 10,415,477 B (9.93 MiB) | 0.382× |
| XERJ after (this change) | 9,028,909 B (8.61 MiB) | 0.331× |
| Elasticsearch 8.13.4 | 27,279,805 B (26.0 MiB) | 1.0× |
The premise did not hold on this corpus. XERJ was already ~2.6× smaller than ES here, not 2× larger. The old "XERJ ≈ 2× ES disk" claim came from a different scenario (almost certainly the un-merged / many-small-segment state during active ingest — XERJ is 16-shard so a freshly-flushed index has ≥16 segments each with per-field FST/meta/norms sidecars, whereas ES auto-merges; and/or a text-heavier corpus). In steady state (force-merged) XERJ wins on disk on this workload.
The safe fix still landed a real 13.3 % steady-state reduction and widened XERJ's lead, with byte-identical round-trip and every gate green.
Phase 1 — Measurement + Diagnosis
Corpus
- 100,000 deterministic docs cycled from
demo/data/extras/chat-events.ndjson, enriched with a real analyzedbodytext field and a high-cardinalitydoc_idkeyword. Mapping: 6 keyword, 1 text, 5 numeric (long/double), 1 date, 1 boolean. Identical bytes ingested into XERJ (:9200) and ES (:9201). - Both force-merged to a single segment (
_forcemerge?max_num_segments=1) + flush, so both are in steady state.
On-disk layout of a XERJ segment
XERJ writes more files per segment than ES but they are individually
addressable (ES bundles everything into one compound .cfs):
| File | Section | Codec |
|---|---|---|
<seg>.seg |
stored _source (columnar V2) + tombstones |
zstd |
<seg>.dv |
doc-values (numeric i64 columns + keyword ordinal columns) | zstd |
<seg>.<field>.post |
FTS positional postings | zstd (ZPS1) |
<seg>.<field>.meta |
per-term metadata (df/ttf/offset/len) | zstd (ZFM4) |
<seg>.<field>.fst |
FST term dictionary | fst crate |
<seg>.<field>.norms |
per-doc field length | raw u16 |
<seg>.ids |
external-id → internal-ord index | raw |
<seg>.sidx |
skip index (written, never read — 16 B) | raw |
Where the bytes are (XERJ, before)
| Component | Bytes | % |
|---|---|---|
.post (postings, 14 fields) |
5,570,604 | 53.5 % |
→ body.post alone |
3,928,612 | 37.7 % |
→ 6 numeric fields' .post |
~1,078,000 | 10.4 % |
.seg (stored _source) |
2,072,355 | 19.9 % |
.dv (doc-values) |
1,819,138 | 17.5 % |
.ids (id index) |
552,154 | 5.3 % |
.meta (term metadata) |
229,560 | 2.2 % |
.norms |
105,486 | 1.0 % |
.fst |
59,987 | 0.6 % |
| Total (disksize index dir) | 10,415,477 | 100 % |
ES stores the equivalent in a single compound file: _3.cfs = 27,278,335 B
(+ 697 B .cfe + 388 B .si + 380 B segments_4).
Root causes of the top contributors
body.post(3.93 MB, 38 %) — FTS positional postings for the one text field: 100k docs × ~30 tokens ≈ 3 M positions. This is inherent to phrase-searchable text and already zstd-compressed; positions can't be dropped without breakingmatch_phrase.- Numeric fields get a full inverted index too (~1.16 MB incl. fst/meta).
context_tokens.post,cost_usd.post,latency_ms.post, … each carry a postings list. ES does not do this for numerics — it uses BKD points + doc-values only. XERJ keyword-analyzes every non-text field into the FST so numerictermqueries and thesize:0count-shortcut can readdoc_freqstraight from the term dictionary. This is a genuine XERJ-vs-ES structural difference and a real redundancy vs. the.dvcolumn — but it is load-bearing (numeric term/count paths read it), so it is not a safe one-pass removal. .seg(2.07 MB) and.dv(1.82 MB) — already zstd, but at the fast flush level (3). The columnar_sourceand the numeric i64 columns are highly redundant and compress ~20–25 % tighter at a higher level.
Everything durable was already zstd level 3 (deliberately: level 19 on the
flush path was reverted in the past because it collapsed sustained-ingest
throughput 70–100× — see the ingest-perf regression report). So "just compress
_source" was already done; the remaining safe lever is spending more
compression effort where it doesn't hurt ingest: the merge path.
Compression-level ceiling (empirical sweep, force-merged 100k)
| zstd level | total | seg | dv | post | meta |
|---|---|---|---|---|---|
| 3 (flush default) | 10,415,477 | 2,072,355 | 1,819,138 | 5,570,604 | 229,560 |
| 9 | 9,789,136 | 1,726,778 | 1,733,544 | 5,406,891 | 198,112 |
| 12 | 9,737,700 | 1,725,519 | 1,698,279 | 5,394,763 | 195,328 |
| 19 | 9,022,248 | 1,561,830 | 1,436,158 | 5,119,175 | 181,281 |
Level 19 is the sweet spot (−13.4 %). The stored/.dv/.meta columns give
~20–25 %; the positional body.post only ~8 % (already high-entropy).
Ranked fix options
| # | Option | Est. saving (this corpus) | Risk | Status |
|---|---|---|---|---|
| 1 | Merge-path zstd 3→19 for .post/.meta/.seg/.dv |
−13.3 % | Very low (lossless, format-identical, flush untouched) | DONE |
| 2 | Drop redundant numeric-field inverted postings | ~11 % here, more on log/metric corpora | Medium — numeric term/count-shortcut must move fully to points/doc-values first |
Follow-up |
| 3 | Delta+bit-pack small-integer .dv columns before zstd |
~2–4 % | Medium — new column format version | Follow-up |
| 4 | Compress/trim .ids id-index (0.55 MB, raw) |
~3–4 % | Low–medium | Follow-up |
| 5 | Stop emitting the never-read .sidx; coalesce 240 tiny WAL files |
negligible bytes, inode win | Low | Follow-up |
Phase 2 — Fix: merge-path zstd level 3 → 19
Idea: keep the fast level 3 on the flush path (ingest-critical, back-pressure-bounded) and raise to level 19 on the merge/forcemerge path, which is background and builds the long-lived steady-state segments. Because a force-merge (and every background compaction) re-encodes the segment, the durable on-disk bytes converge on the merge-level output regardless of flush level.
Why it is safe (correctness-paramount):
- zstd decompression is level-independent — the on-disk frame format is identical at any level, so every reader is byte-for-byte unchanged and old and new segments interoperate. A plumbing mistake can at worst cost flush CPU; it can never corrupt or lose data.
- All existing flush call sites keep passing the fast level (3), so sustained-ingest throughput is untouched (the past zstd-19 regression was a flush-path regression).
Files changed
engine/crates/xerj-storage/src/doc_values.rsDV_FLUSH_ZSTD_LEVEL=3,DV_MERGE_ZSTD_LEVEL=19(585, 592);encode_columns→ thin wrapper over newencode_columns_level(cols, level)(597, 604).
engine/crates/xerj-storage/src/stored_codec.rsSTORED_MERGE_ZSTD_LEVEL=19(129);encode_stored_v2→ wrapper over newencode_stored_v2_level(bytes, level)(160, 167);levelthreaded throughencode_stored_v2_from_values_inner/encode_v2_columns/encode_dict_bitpack/encode_cross_dep; the flushfrom_values*entries pinSTORED_ZSTD_LEVEL(3).
engine/crates/xerj-fts/src/index.rsMERGE_ZSTD_LEVEL=19(204);FtsIndexWriter.zstd_levelfield (default 3) +set_zstd_level()(469); threaded throughfinish→write_field_static(720) andencode_field_meta_v4(241) for both.postand.meta.
engine/crates/xerj-engine/src/index.rs- Merge path:
encode_stored_v2_level(…, STORED_MERGE_ZSTD_LEVEL)(2841),fts_writer.set_zstd_level(MERGE_ZSTD_LEVEL)(2871),write_doc_values_sidecar(…, DV_MERGE_ZSTD_LEVEL)(2938). - Flush path:
write_doc_values_sidecar(…, DV_FLUSH_ZSTD_LEVEL)(10698); flush FTS writer keeps the default level.write_doc_values_sidecargained alevelparameter (8070).
- Merge path:
Results (100k, force-merged)
| Component | before (L3) | after (L19) | Δ |
|---|---|---|---|
.seg |
2,072,355 | 1,561,105 | −24.7 % |
.dv |
1,819,138 | 1,441,697 | −20.7 % |
.post |
5,570,604 | 5,119,926 | −8.1 % |
→ body.post |
3,928,612 | 3,553,174 | −9.6 % |
.meta |
229,560 | 182,361 | −20.6 % |
| Total | 10,415,477 | 9,028,909 | −13.3 % |
New ratio vs ES: 0.331× (was 0.382×).
Correctness evidence
- Round-trip
_source: 300 sampled docs read back from the level-19 merged segment — every field value byte-identical to the generated doc. 0 value mismatches. (XERJ's columnar codec reorders_sourcekeys; that is pre-existing and orthogonal to the lossless level change.) - Query parity vs ES: 10/10 identical hit totals — term (keyword/numeric/
bool), range (long/double),
match,match_phrase, bool, exists. - Unit tests:
cargo test -p xerj-engine -p xerj-storage -p xerj-ftsgreen (includes stored-codec parity + numeric/keyword round-trip tests). - ES-YAML suite: 1360 passed · 0 failed · 3 skipped (target held).
- Read latency A/B (level-19 :9200 vs level-3 :9202, identical data): search
paths sub-ms on both; the decompression-sensitive
match_phrase bodyis identical (0.35/1.17 vs 0.30/1.16 ms p50/p99); the_doc/{id}slow path (~918 ms, pre-existing) is identical on both levels → no decompression regression.
Caveats / risks
- Background merge CPU rises ~8× per merge (level 19 ≈ 25 MB/s/core vs level 3 ≈ 250). Merges are background and not back-pressure-gated, but under sustained max ingest slower merges could let tier-0 segment count grow transiently. Flush throughput itself is unchanged. If this ever bites, drop the merge level to 12 (captures ~half the win at ~3× flush CPU) — it is a single constant per crate.
- Forward/backward compatible: no format/magic change; level is purely an encode-effort knob. Mixed-level segments coexist and read identically.
- The numeric-postings redundancy (option 2) is the larger structural lever, especially on log/metric corpora where numerics dominate — but it changes what the read path may rely on and must be done carefully; left as a follow-up.