All Products
Search
Document Center

OpenSearch:BM25F

Last Updated:Oct 19, 2026

Overview

BM25F extends BM25 to calculate the text relevance of query terms across multiple fields. You can assign a different weight to each field, and BM25F combines the BM25 scores of the fields. The BM25F formula is as follows:12

Functions

Function prototype

Description

BM25F create(OpsScorerInitParams params,CString indexName)

A factory function that creates a BM25F object for the specified index.

BM25F create(OpsScorerInitParams params,CString indexName, CString[] fields)

A factory function that creates a BM25F object for the specified index and the specified fields in the index.

void setGroupScoreMergeOp(CString opName)

Specifies how to merge the results of multiple query groups. Valid values: sum and max. Default value: sum.

void setParamK(double paramK)

Sets the K parameter.

void setFieldParamB(CString fieldName, double paramB)

Sets the B parameter for a field.

void setTotalDocNum(long totalDocNum)

Sets the total number of documents.

void setFieldAvgLength(CString fieldName, int avgFieldLength)

Sets the average length of a field.

void setFieldWeight(CString fieldName, double fieldWeight)

Sets the weight of a field.

double evaluate(OpsScoreParams params)

Calculates how well the query terms match the specified fields.

Function details

BM25F create(OpsScorerInitParams params,CString indexName)

Creates a BM25F object. You must specify the name of the index to be matched. If no field names are specified, all fields in the index are used in the BM25F calculation by default. Parameters: params — the initialization input parameters. For more information, see OpsScorerInitParams. indexName — the name of the index. The value must be a constant.

BM25F create(OpsScorerInitParams params,CString indexName, CString[] fields)

Creates a BM25F object. You must specify the name of the index to be matched and the list of fields used in the calculation. Parameters: params — the initialization input parameters. For more information, see OpsScorerInitParams. indexName — the name of the index. The value must be a constant. fields — the list of fields used in the BM25F calculation.

void setGroupScoreMergeOp(CString opName)

Specifies the rule for combining the scores of multiple query groups. Only max and sum are supported. If this rule is not specified, the scores of multiple groups are summed by default. This function must be called during the initialization phase of the scoring plug-in. Query groups are expansions of the original query terms produced by query analysis. By default, only one query group exists. Parameters: opName — the rule for combining the scores of multiple query groups. Valid values: max and sum.

void setParamK(double paramK)

Sets the value of the 1 parameter. This function must be called during the initialization phase of the scoring plug-in. Parameters: paramK — the value of 1. Default value: 2.0.

void setFieldParamB(CString fieldName, double paramB)

Sets the value of the B parameter used to calculate BM25 for a field. This function must be called during the initialization phase of the scoring plug-in. Parameters: fieldName — the name of the field. The field must exist in the specified index. If a field list was specified when the BM25F object was created, fieldName must be included in that list. The value must be a string constant. paramB — the value of b. Default value: 0.1.

void setTotalDocNum(long totalDocNum)

Sets the number of documents in the application. This function must be called during the initialization phase of the scoring plug-in. Parameters: totalDocNum — the number of documents. Default value: 92000000.

void setFieldAvgLength(CString fieldName, int avgFieldLength)

Sets the average length of a field. This function must be called during the initialization phase of the scoring plug-in. Parameters: fieldName — the name of the field. The field must exist in the specified index. If a field list was specified when the BM25F object was created, fieldName must be included in that list. The value must be a string constant. avgFieldLength — the average length of the field. Default value: 20.

void setFieldWeight(CString fieldName, double fieldWeight)

Sets the weight of a field. This function must be called during the initialization phase of the scoring plug-in. Parameters: fieldName — the name of the field. The field must exist in the specified index. If a field list was specified when the BM25F object was created, fieldName must be included in that list. The value must be a string constant. fieldWeight — the weight of the field. Default value: 1.0.

double evaluate(OpsScoreParams params)

Calculates the BM25F value. Parameters: params — the scoring input parameters. For more information, see OpsScoreParams. Return value: the BM25F score. Valid values: [0, 1]. Sample code:

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.features.similarity.fieldmatch.BM25F;

class BasicSimilarityScorer {
    BM25F _f1;
    boolean init(OpsScorerInitParams params) {
        CString[] fields1 = {"title", "body"};
        _f1 = BM25F.create(params, "default", fields1);
        _f1.setFieldAvgLength("title", 10);
        _f1.setFieldWeight("title", 10D);
        _f1.setFieldParamB("title", 0.6);

        _f1.setFieldAvgLength("body", 100);
        _f1.setFieldWeight("body", 2D);
        _f1.setFieldParamB("body", 0.5);
        return true;
    }

    double score(OpsScoreParams params) {
        return _f1.evaluate(params);
    }
};