Overview
The FieldTermMatchCount class is used to calculate the number of terms that match a search query in a 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
. In this case, the number of terms that match the search query in the title field is 2.
Functions
Function | Description |
FieldTermMatchCount create(OpsScorerInitParams params, CString indexName, CString fieldName) | Creates a FieldTermMatchCount object. This function is a factory function. |
void setGroupScoreMergeOp(CString opName) | Sets an aggregation method for the FieldTermMatchCount scores of multiple query groups. Supported aggregation methods are sum and max. The default aggregation method is sum. |
double evaluate(OpsScoreParams params) | Calculates the number of terms that match a search query in a field. |
Function details
FieldTermMatchCount create(OpsScorerInitParams params, CString indexName, CString fieldName)
Creates a FieldTermMatchCount object based on an index and a 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, an 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 FieldTermMatchCount 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 number of terms that match a search query in a field. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: the number of terms that match a search query in a field. 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.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);
}
}