All Products
Search
Document Center

OpenSearch:FieldMatchRatio

Last Updated:Feb 20, 2023

Overview

The FieldMatchRatio class is used to calculate the ratio of terms that a search query hits in a specific field to all terms in the field. The ratio indicates the proximity of the search query to the specified field. For example, the terms in the title field after analysis are field, match, ratio, user, guide, and the terms in a search query after analysis are OpenSearch, user, guide. 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. The score is calculated by dividing the number of terms that the search query hits in the title field by the total number of terms in the title field.

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 specific field to all terms in the field.

Function details

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

Creates a FieldMatchRatio object based on a specific index and a specific field. This function is a factory function. 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. The sum operator 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 specific 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);
    }
}