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:

i n I D F ( q i ) f ( q i , D ) ( k 1 + 1 ) f ( q i , D ) + k 1 ( 1 - b + b f i e l d _ l e n a v g _ f i e l d _ l e n )
Where the components in the function are:

  • I D F ( q i ) : The inverse document frequency (IDF) of query term i in field t. This is calculated as:

    l o g ( 1 + N - n ( q i ) + 0.5 n ( q i ) + 0.5 )
    N is the total number of documents on the content node. n ( q i ) 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 ( q i , D ) : 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.
  • f i e l d _ l e n : 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.
  • a v g _ f i e l d _ l e n : 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