WRITE · TRACE · UNDERSTAND

Project: rank an authorized corpus with real BM25 statistics

Documents have tenant,id,roles,text; (tenant,id) must be unique.

Read lesson: Project: rank an authorized corpus with real BM25 statistics →

Your experiment

Documents have tenant,id,roles,text; (tenant,id) must be unique. Admit only the requested tenant and public documents (roles empty) or any role intersection. Tokenize query and documents using lowercase ASCII [a-z0-9]+; deduplicate query terms. Compute all corpus statistics after permission filtering. BM25 uses k1=1.2,b=.75, idf=log(1+(N-df+.5)/(df+.5)), term contribution=idf*tf*2.2/(tf+1.2*(.25+.75*length/average_length)). Return positive-score {id,score} rows sorted by descending score then id, cut at integer k>=0. Empty/no-match/empty-length corpora return [].

Python runs in your browser. Your draft stays on this device.

What to look for

Review tenant/ACL provenance, corpus-statistic scope, tokenizer changes, ranking evaluation and tie stability. Production indexes also need versioned updates, revocation handling and access-aware caches. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: rank an authorized corpus with real BM25 statistics works

The idea

BM25 is the standard formula for keyword search. A document scores higher when it contains a query term more often, with diminishing returns set by k1, and long documents are discounted through b so that length alone does not win. Each term is weighted by its inverse document frequency: a word found in few documents says more than a word found in most.

Those weights are statistics of the corpus, and this project is built around that fact. The document count, the document frequencies and the average length are all computed after removing the documents the caller may not see. If hidden documents were counted, the scores of visible ones would move with them, and a caller could learn something about text they are not allowed to read.

How it works

  1. Reject a bad k and duplicate (tenant, id) pairs, then keep documents of the caller's tenant that are public or share a role.
  2. Tokenize the query and each allowed document into lowercase [a-z0-9]+ words, and deduplicate the query terms.
  3. From the allowed documents only, compute each length, the average length, and how many documents contain each term.
  4. For each term compute idf = log(1 + (N - df + 0.5) / (df + 0.5)).
  5. Sum idf * tf * 2.2 / (tf + 1.2 * (0.25 + 0.75 * length / average)) over the query terms.
  6. Keep positive scores, sort by descending score and then id, and return the first k rows.

What it costs

Time
O(corpus tokens + N·q)
Space
O(corpus tokens)

This answer tokenizes all N allowed documents on every call for q query terms; search engines build an inverted index once, so a query touches only documents that contain its terms.

Where it is used

  • Apache Lucene has used BM25 as its default similarity since version 6.0, so Elasticsearch and OpenSearch rank text with it by default.
  • SQLite's FTS5 extension has a built-in bm25() function for ordering full-text matches.
  • Anserini and its Python interface Pyserini provide the BM25 baselines reported in many information retrieval papers.

Watch out for

  • Computing N, df or the average length over all documents and filtering afterwards: hidden documents then change the visible scores.
  • Scoring by counting shared words: the check expects log(4/3) for a single matching document, which only the real formula gives.
  • Leaving repeated query words in: SAME same must count as one term, or its contribution is added twice.