# BM25 Reference

The bm25 rank feature implements the Okapi BM25 ranking function used to estimate the relevance of a text document given a search query. It is a pure text ranking feature which operates over an indexed string field. The feature is very cheap to compute, about 3-4 times faster than nativeRank, while still providing a good rank score quality wise. It is a good candidate to use in a first phase ranking function when ranking text documents.

## Ranking function

The bm25 feature calculates a score for how good a query with terms ${q}_{1},...,{q}_{n}$ matches an indexed string field t in a document D. The score is calculated as follows:

Where the components in the function are:

• $IDF\left({q}_{i}\right)$: The inverse document frequency (IDF) of query term i in field t. This is calculated as:

N is the total number of documents on the content node. $n\left({q}_{i}\right)$ is the number of documents containing query term i for field t, which is calculated per index existing for that field. The max value among the indexes is used in the calculation, which typically comes from the largest disk index.

As the IDF is calculated per content node and index, slight variations might occur. To use the same IDF across all content nodes, set it as the significance on each query term using annotations.

• $f\left({q}_{i},D\right)$: The number of occurrences (term frequency) of query term i in the field t of document D. For multi-value fields we use the sum of occurrences over all elements.
• $field_len$: The field length (in number of words) of field t in document D. For multi-value fields we use the sum of field lengths over all elements.
• $avg_field_len$: The average field length of field t among the documents on the content node.
• ${k}_{1}$: A parameter used to limit how much a single query term can affect the score for document D. With a higher value the score for a single term can continue to go up relatively more when more occurrences for that term exists. Default value is 1.2. Can be configured using rank-properties.
• $b$: A parameter used to control the effect of the field length of field t compared to the average field length. Default value is 0.75. Can be configured using rank-properties.

## Example

In the following example we have an indexed string field content, and a rank profile using the bm25 rank feature. Note that the field must be enabled for usage with the bm25 feature by setting the enable-bm25 flag in the index section of the field definition.

schema example {
document example {
field content type string {
indexing: index | summary
index: enable-bm25
}
}
rank-profile default {
first-phase {
expression {
bm25(content)
}
}
}
}


If the enable-bm25 flag is turned on after documents are already fed, some extra steps must be executed to prepare the posting lists in the memory and disk indexes for this field. For each content node do the following:

1. Perform memory index flush using vespa-proton-cmd:
vespa-proton-cmd --local triggerFlush

2. Perform disk index fusion, preparing the posting lists for use with bm25:
vespa-proton-cmd --local triggerFlush