All Products
Search
Document Center

FieldMatchRatio

Last Updated: Sep 09, 2021

Overview

The FieldMatchRatio class is used to calculate the ratio of terms that a search query hits in a specified field to all terms in the field. The ratio indicates the proximity of the search query to the specified field. For example, the terms of the title field after analysis are field, match, ratio, User, and Manual. The terms in a search query are OpenSearch, User, and Manual. The number of terms that the search query hits in the title field is 2. The total number of terms in the title field is 5. Therefore, the FieldMatchRatio score of the search query in the title field is 0.4.

Functions

Function

Description

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

Creates a FieldMatchRatio object.

void setGroupScoreMergeOp(CString opName)

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

double evaluate(OpsScoreParams params)

Calculates the ratio of terms that a search query hits in a specified field to all terms in the field.

Function details

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

Creates a FieldMatchRatio object based on a specified index and a specified field. 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. 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. 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.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the FieldMatchRatio scores of multiple query groups. This function must be invoked in the init function. Parameter: opName: the name of an operator. Supported operators are sum and max. sum: calculates a sum of multiple scores. The max operator returns the maximum value among multiple scores. The default value is sum.

double evaluate(OpsScoreParams params)

Calculates the ratio of terms that a search query hits in a specified field to all terms in the field. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: the FieldMatchRatio score of the search query in the specified field. 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.FieldMatchRatio;

class BasicSimilarityScorer {
    FieldMatchRatio _fieldMathcRatio;
    boolean init(OpsScorerInitParams params) {
        _fieldMatchRatio = FieldMatchRatio.create(params, "title_index", "title");
        _fieldMatchRatio.setGroupScoreMergeOp("max");
        return true;
    }

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