Overview
The QueryTermMatchCount class is used to calculate the number of terms that a search query hits in indexes. In OpenSearch, an index can contain multiple fields. If a search query hits a term in one of the fields of an index, the corresponding document is returned in results. The QueryTermMatchCount class supports two calculation modes. In one mode, this class calculates the number of terms that a search query hits in all fields of an index. In the other mode, this class calculates the number of terms that a search query hits in a specified field of an index. For example, the default index contains two fields: title and body. A search query is default:'User Manual'. You can calculate the number of terms that the search query hits in both the title and body fields. You can also calculate the number of terms that the search query hits in only the title field or only the body field.
Functions
Function | Description |
---|---|
QueryTermMatchCount create(OpsScorerInitParams params) | Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in all indexes. |
QueryTermMatchCount create(OpsScorerInitParams params, CString indexName) | Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in all fields of the specified index. |
QueryTermMatchCount create(OpsScorerInitParams params, CString indexName, CString fieldName) | Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in the specified field of the specified index. |
void setGroupScoreMergeOp(CString opName) | Sets an aggregation method for the QueryTermMatchCount 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 a search query hits. |
Function details
QueryTermMatchCount create(OpsScorerInitParams params)
Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in all indexes.
QueryTermMatchCount create(OpsScorerInitParams params, CString indexName)
Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in all fields of the specified index. indexName: the name of an index. The name must be a constant.
QueryTermMatchCount create(OpsScorerInitParams params, CString indexName, CString fieldName)
Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in the specified field of the specified index. indexName: the name of an index. The name must be a constant. 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. 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.
void setGroupScoreMergeOp(CString opName)
Sets an aggregation method for the QueryTermMatchCount scores of multiple query groups. Supported aggregation methods are sum and max. The default aggregation method is sum. This function can be invoked only during the initialization of a score calculation object. Query groups are generated after the original search query is processed by an analyzer. By default, only one query group exists.
opName: the method that is used to aggregate the QueryTermMatchCount scores of multiple query groups. Supported aggregation methods are max and sum.
double evaluate(OpsScoreParams params)
Calculates the number of terms that a search query hits. params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: the number of terms that the search query hits. 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.querymatch.QueryTermMatchCount;
class BasicSimilarityScorer {
QueryTermMatchCount _f1;
boolean init(OpsScorerInitParams params) {
_f1 = QueryTermMatchCount.create(params, "pack_index2", "text_field1");
return true;
}
double score(OpsScoreParams params) {
return _f1.evaluate(params);
}
};