All Products
Search
Document Center

OpenSearch:QueryTermMatchCount

Last Updated:Feb 06, 2023

Overview

The QueryTermMatchCount class is used to calculate the number of terms that a search query hits in an index. In OpenSearch, an index can contain multiple fields. If a term in 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 specific 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 fields of the default index.

QueryTermMatchCount create(OpsScorerInitParams params, CString indexName)

Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in a specific field of a specific 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 a specific field of a specific 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 ratio of terms that a search query hits in a specific field or all fields of an index to all terms in the index.

Function details

QueryTermMatchCount create(OpsScorerInitParams params)

Creates a QueryTermMatchCount object that calculates the number of terms that a search query hits in all fields of the default index.

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 a specific index. Parameter: 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 a specific field of a specific index. Parameter: 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 number of terms of multiple query groups. Supported aggregation methods are sum and max. The default aggregation method is sum. This function can be called 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.

Parameter: opName: the method that is used to aggregate the number of terms of multiple query groups. Supported aggregation methods are max and sum.

double evaluate(OpsScoreParams params)

The QueryTermMatchCount class is used to calculate the number of terms that a search query hits in an index. Parameter: 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 in the index. 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);
    }
};