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
| Function | Description |
|---|---|
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
| Parameter | Description |
|---|---|
params | Initialization parameters. For more information, see OpsScorerInitParams. |
indexName | The 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. |
fieldName | The 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
| Parameter | Description |
|---|---|
opName | The 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
| Parameter | Description |
|---|---|
params | Score 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);
}
}