All Products
Search
Document Center

OpenSearch:FieldMatchWeighted

Last Updated:Feb 06, 2023

Overview

The FieldMatchWeighted class is used to calculate the FieldMatchWeighted score of a search query. The score indicates the extent to which the search query matches the specified field in the specified index. The more terms that a search query hits in the specified field, the higher the weighted score and the FieldMatchWeighted score are. The FieldMatchWeighted score of a search query consists of baseScore and bonusScore. The score is calculated by using the following formula: (A × baseScore + bonusScore)/(A + 1). Variable A indicates the weight of baseScore. The following figure shows the calculation logic of baseScore.image tw indicates the weight of a term. The value is generated after query analysis. The default value is 1. bonusScore is a weighted score. bonusScore takes effect only when a search query exactly matches a field or a search query is included in a field. You can call the functions that are provided for FieldMatchWeighted objects to set Variable A and bonusScore.

Functions

Function

Description

FieldMatchWeighted create(OpsScorerInitParams params, CString indexName, CString fieldName)

Creates a FieldMatchWeighted object.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the FieldMatchWeighted scores of multiple query groups. Supported aggregation methods are sum and max. The default aggregation method is sum.

void setParamA(double paramA)

Sets Variable A.

void setExactMatchBonus(double exactMatchBonus)

Sets the weighted score when a search query exactly matches a field.

void setNgramMatchBonus(double ngramMatchBonus)

Set the weighted score when a search query is included in a field.

double evaluate(OpsScoreParams params)

Calculates the proximity of a query term to a specific field.

Function details

FieldMatchWeighted create(OpsScorerInitParams params, CString indexName, CString fieldName)

Creates a FieldMatchWeighted object. When you call this function, you must specify an index name and a field name. Parameters: params: the parameters that are used for initialization. For more information, see OpsScorerInitParams. indexName: the name of an index. The name must be a constant. fieldName: the name of a field in the specified index. The name must be a constant. The field must be of the TEXT or SHORT_TEXT type. The analyzer can be the general analyzer for Chinese, a custom analyzer, the single character analyzer for Chinese, the analyzer for English, or the analyzer for fuzzy searches.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the FieldMatchWeighted scores of multiple query groups. Supported aggregation methods are sum and max. The default aggregation method is sum. This function can be called only during the initialization of a score calculation object. Query groups are generated after the original search query is processed by an analyzer. By default, only one query group exists. Parameter: opName: the method that is used to aggregate the FieldMatchWeighted scores of multiple query groups. Supported aggregation methods are max and sum.

void setParamA(double paramA)

Sets Variable A. This parameter specifies the weight of baseScore in the overall score. This function can be called only during the initialization of a score calculation object. Parameter: paramA: the value to be assigned to Variable A. Default value: 0.5.

void setExactMatchBonus(double exactMatchBonus)

Sets the weighted score when a search query exactly matches a field. This function can be called only during the initialization of a score calculation object. Parameter: exactMatchBonus: the weighted score. Default value: 1.0.

void setNgramMatchBonus(double ngramMatchBonus)

Sets the weighted score when a search query is included in a field. This function can be called only during the initialization of a score calculation object. Parameter: ngramMatchBonus: the weighted score. Default value: 0.6.

double evaluate(OpsScoreParams params)

Calculates the extent to which a search query matches the specified field in the specified index. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: The text relevance between the search query and the specified field in the specified index. 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.FieldMatchWeighted;

class BasicSimilarityScorer {
    FieldMatchWeighted _f1;
    boolean init(OpsScorerInitParams params) {
        _f1 = FieldMatchWeighted.create(params, "title_index", "title");
        _f1.setGroupScoreMergeOp("max");
        _f1.setParamA(1);
        _f1.setExactMatchBonus(0.5);
        _f1.setNgramMatchBonus(0.3);
        return true;
    }

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