runi/bm25

import "github.com/org-runink/runi/bm25"

Package bm25 ranks documents against a query by the words they share.

BM25 is the ranking function most search engines were built on before embeddings, and it remains the strongest thing you can run with no model, no vector store, and no GPU. It scores a document higher when it contains the query's rarer words, more often, in fewer total words.

  • Exact terms matter: identifiers, SKUs, error codes, names, version numbers. An embedding of "ERR-4021" is near the embedding of "ERR-4022", which is exactly wrong.
  • You need to explain a result. [Result.Terms] reports which query terms matched and what each contributed, so a ranking can be justified rather than asserted.
  • The corpus changes constantly. Indexing is a pass over the text; there is nothing to re-embed.
  • There is no budget for a model in the request path.

When it does not

BM25 matches words, not meaning. A query for "car" will not find a document that only says "automobile". If your users paraphrase, BM25 alone will disappoint them — the usual answer is to run both and combine the rankings.

Scores are not probabilities

A BM25 score is only meaningful compared with other scores for the SAME query. It is unbounded above and depends on corpus statistics, so "score > 0.8" is not a usable relevance threshold and a score from one query cannot be compared with a score from another. Rank, then cut by position.

Measured against rank_bm25 and scikit-learn

5,000 synthetic documents, 200 queries:

                    bm25    rank_bm25   sklearn TfidfVectorizer

index 5,000 docs 19.3 ms 93.5 ms 171.3 ms 200 queries 3.92 ms 628.3 ms -

4.8x faster to index than rank_bm25, 8.9x faster than building a TF-IDF matrix with scikit-learn, and 160x faster to query.

Both numbers are mostly about allocation rather than arithmetic. The build does 65 allocations where it once did 20,031: documents emit (term, count) pairs into one flat buffer, and the postings arena is filled by a counting sort instead of appending to 2,000 separately growing slices. Search does 4,800 where it once did 337,826: the per-document score breakdown is now rebuilt only for the documents actually returned, and the top-k comes off a bounded heap rather than a full sort of every candidate -- which mattered more than it sounds, because on a corpus of equal-length documents nearly every pair of candidates ties on score and the comparison falls through to comparing document IDs.

Measured on an ASUS Ascent GX10, 20 cores, aarch64, Go 1.27.2, with rank-bm25 and scikit-learn 1.9.1 on Python 3.12.3.

Index

Document

type Document struct

Document is one indexed item.

Defined in bm25/bm25.go.

Index

type Index struct

Index is a searchable corpus. Build it with [New]; it is read-only and safe for concurrent use once built.

The zero value is an empty index: Len reports 0 and Search returns nil.

Defined in bm25/bm25.go.

Options

type Options struct

Options configures an [Index]. The zero value uses the defaults above.

Defined in bm25/bm25.go.

Result

type Result struct

Result is one ranked hit.

Defined in bm25/bm25.go.

Index.Len

func (ix *Index) Len() int

Len reports how many documents are indexed.

Defined in bm25/bm25.go.

Index.Search

func (ix *Index) Search(query string, k int) []Result

Search returns the top-k documents for a query, highest score first. A k of zero or less returns every document that matched at least one term.

Ties are broken by document ID so that the same corpus and query always produce the same order — an unstable ranking makes a result impossible to reproduce in a bug report.

Defined in bm25/bm25.go.

New

func New(docs []Document, opts Options) *Index

New builds an index over docs. Documents with empty text are indexed with length zero and can never match, which is deliberate: dropping them would make the returned corpus size disagree with the input.

Defined in bm25/bm25.go.

SimpleTokenise

func SimpleTokenise(s string) []string

SimpleTokenise lowercases text and splits it on anything that is not a letter or a digit. It keeps digits joined to letters, so "err-4021" yields "err" and "4021" while "v2" stays "v2".

It does no stemming: "run" and "running" are different terms. Stemming helps recall and costs precision, and which you want depends on the corpus, so it is left to a custom Tokenise rather than imposed.

Defined in bm25/bm25.go.

results matching ""

    No results matching ""