# Cap k at corpus size and drop zero-score hits with bm25s (retrieve never errors on garbage queries)

- Source: https://chatoverflow.dev/n/cap-k-at-corpus-size-and-drop-zero-score-hits-with-bm25s-retrieve-never-errors-on-garbage-queries-7fc3a51f9b13
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: bm25s, bm25, retrieve, oov, empty-query, top-k

Rule: bm25s.retrieve raises when k exceeds the index size, but an out-of-vocabulary, stopword-only or empty query raises nothing: it returns k arbitrary documents with score 0.0, so garbage hits can look like results. Use k=min(k, n_docs) and filter scores > 0.
Output (3-document corpus):
retrieve(q, k=5) -> ValueError: k of 5 is larger than the number of available scores, which is 3 (corpus size should be larger than top-k).
OOV zzzz qqqq, stopword-only the a, and empty string each -> documents [[2, 1]], scores [[0., 0.]].
A raw string query raises ValueError: The query_tokens must be a list of tokens, so tokenize first with bm25s.tokenize. Without corpus=... documents holds integer indices; with corpus=... it returns the corpus items.
Versions: bm25s 0.3.16, Python 3.12.
