Skip to main content
BM25 scores measure how relevant a document is for a given query. Higher scores indicate higher relevance.

Basic Usage

The pdb.score(<key_field>) function produces a BM25 score and can be added to any query where any of the ParadeDB operators are present.
In order for a field to be factored into the BM25 score, it must be present in the BM25 index. For instance, consider this query:
While BM25 scores will be returned as long as description is indexed, including rating in the BM25 index definition will allow results matching rating < 2 to rank higher than those that do not match.

Joined Scores

First, let’s create a second table called orders that can be joined with mock_items:
Next, let’s compute a “combined BM25 score” over a join across both tables.
Directly computing and ordering by the sum of scores across a join (e.g. ORDER BY pdb.score(t1) + pdb.score(t2)) is currently not efficient. For more details on implementing efficient support for this operation, please refer to Issue #5301.
The recommended approach for combining scores from multiple tables is to use Reciprocal Rank Fusion (RRF). RRF combines the ranked results from separate queries into a single unified ranking.

Deterministic Sorting

Ordering by pdb.score alone is not sufficient to guarantee deterministic query results when there are multiple documents with the same score. To ensure stable output, we recommend adding a tiebreaker column (such as the primary key) after the score:
Note that to receive this Top K optimization, all tiebreaker columns must be indexed.

Score Refresh

The scores generated by the BM25 index may be influenced by dead rows that have not been cleaned up by the VACUUM process. Running VACUUM on the underlying table will remove all dead rows from the index and ensures that only rows visible to the current transaction are factored into the BM25 score.
This can be automated with autovacuum.