Files
AnkurandClaude Sonnet 5.5 98ed878675 Add rag-eval module: retrieval metrics, golden dataset and a JUnit regression gate
hit@k, recall@k, precision@k, MRR and nDCG@k in plain Java with their edge
cases pinned by tests, a 30-question graded golden dataset over a 40-passage
runbook, a paired bootstrap, and a gate (baseline + tolerance + no new misses)
shown failing on a tokenizer that drops digits. BM25, a hashed-trigram vector
(not a neural embedding) and RRF; no model, no network. Java 25, JUnit 6.1.3.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7F47n8shUHpexxavn
2026-11-25 13:20:18 +05:30

4.4 KiB

rag-eval

Companion code for Measuring RAG Retrieval Quality in Java: recall@k, MRR, nDCG and a Golden Dataset in JUnit, part of the Spring AI series on ankurm.com.

Retrieval metrics in plain Java (recall@k, precision@k, hit@k, MRR, nDCG@k), a 30-question golden dataset over a 40-passage support runbook, a paired bootstrap for "is this change real?", and a JUnit gate that fails the build on a retrieval regression. Everything the article explains is in this module; the article carries the explanations in expandable sections, so there is no separate docs folder.

No model and no network are used. The retrievers are BM25, a hashed character-trigram vector (HashedNgramRetriever, a stand-in that captures spelling overlap and not meaning, not a neural embedding) and Reciprocal Rank Fusion of the two. The corpus and the questions are synthetic and were written for this module, so the scores say how the code behaves, not how any real embedding model or any real corpus would score. To evaluate a real retriever, implement Retriever around it; nothing else changes.

Versions

Component Version
Java 25 (LTS)
JUnit 6.1.3 (latest GA on Maven Central on 2026-11-25)
Maven Surefire 3.5.4

No Spring, no other dependency.

Quickstart

scripts/run-all.sh          # 10 tests; regenerates output/00 .. 08
mvn test                    # the gate on its own: passes
mvn test -Dtest=GoldenSetGateTest -Drageval.retriever=bm25-lettersonly   # the gate failing, on purpose
scripts/write-baseline.sh bm25 0.04   # re-lock the baseline after an improvement you want to keep

Two consecutive runs produce byte-identical files.

What's here

File What it is
Metrics.java hit@k, recall@k, precision@k, reciprocal rank and nDCG@k as pure functions, with the edge-case rules in the Javadoc
Evaluator.java, Report.java Run a Retriever over the golden set; per-question scores, macro means, per-category means, misses
Bootstrap.java Paired bootstrap over questions: a 95% interval for the difference between two retrievers
Gate.java The CI check: a baseline with a tolerance, plus "no new question may miss in the top 5"
Bm25Retriever.java, HashedNgramRetriever.java, RrfRetriever.java, Tokenizer.java The retrievers under test, and the tokenizer whose LETTERS_ONLY mode is the regression the gate is shown catching
corpus.tsv, golden.tsv The passages and the judged questions (grade 2 answers it alone, grade 1 is supporting context)
baseline.properties The scores the gate compares against, written by WriteBaseline

Output files

File Written by
00-dataset.txt DatasetTest (also validates every judgement points at a real passage)
01-metric-worked-examples.txt MetricsTest
02-pitfalls.txt PitfallsTest
03-retriever-comparison.txt RetrieverComparisonTest
04-bootstrap.txt BootstrapTest
05-misses.txt MissAnalysisTest
06-gate-pass.txt GoldenSetGateTest
07-gate-catches-regression.txt GateCatchesRegressionTest
08-ci-failure.txt scripts/ci-fail-demo.sh, the recorded output of a failing gate

Limits

30 questions is small: one question moving one rank moves MRR by up to 0.033, and no difference between the retrievers here has a bootstrap interval that excludes zero. Treat the scores as a demonstration of the method, not a ranking of the retrievers.