FieldTermMatchCount

更新时间:
复制 MD 格式

FieldTermMatchCount counts how many terms in a search query match terms in a specified field after analysis. For example, if the title field contains the analyzed terms field, match, ratio, user, and guide, and the search query produces the analyzed terms OpenSearch, user, and guide, the match count is 2.

Functions

FunctionDescription
FieldTermMatchCount create(OpsScorerInitParams params, CString indexName, CString fieldName)Creates a FieldTermMatchCount object for a specified index and field. This is a factory function.
void setGroupScoreMergeOp(CString opName)Sets how scores from multiple query groups are merged. Supported operators: sum (default) and max.
double evaluate(OpsScoreParams params)Returns the number of terms that match the search query in the specified field.

Function details

create

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

Creates a FieldTermMatchCount object based on a specified index and field. This is a factory function.

Parameters

ParameterDescription
paramsInitialization parameters. For more information, see OpsScorerInitParams.
indexNameThe name of the index. Must be a constant. Supported analyzers: general analyzer for Chinese, custom analyzer, single character analyzer for Chinese, analyzer for English, and analyzer for fuzzy searches.
fieldNameThe name of a field in the specified index. Must be a constant. The field must be of the TEXT or SHORT_TEXT type.

setGroupScoreMergeOp

void setGroupScoreMergeOp(CString opName)

Sets the aggregation operator for merging FieldTermMatchCount scores across multiple query groups. Call this function inside the init function.

Parameters

ParameterDescription
opNameThe aggregation operator. Valid values: sum (calculates the sum of all group scores), max (returns the highest score among all groups). Default value: sum.

evaluate

double evaluate(OpsScoreParams params)

Returns the number of terms that match the search query in the specified field.

Parameters

ParameterDescription
paramsScore calculation parameters. For more information, see OpsScoreParams.

Return value: The number of matching terms as a double.

Example

The following example creates a FieldTermMatchCount scorer that uses the title field of title_index and applies the max operator to merge scores across query groups.

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.FieldTermMatchCount;

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

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