All Products
Search
Document Center

QueryMatchRatio

Last Updated: Sep 09, 2021

Overview

The QueryMatchRatio class is used to calculate the ratio of terms that a search query hits in a specified field or all fields of an index to all terms in the 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 QueryMatchRatio class supports two calculation modes. In one mode, this class calculates the ratio of terms that a search query hits in all fields of an index to all terms in the index. In the other mode, this class calculates the ratio of terms that a search query hits in a specified field of an index to all terms in the index. For example, the default index contains two fields: title and body. A search query is default:'User Manual'. You can calculate the ratio of terms that the search query hits in both the title and body fields to all terms in the default index. You can also calculate the ratio of terms that the search query hits in only the title field or only the body field to all terms in the default index.

Functions

Function

Description

QueryMatchRatio create(OpsScorerInitParams params)

Creates a QueryMatchRatio object that calculates the ratio of terms that a search query hits in all fields of the default index to all terms in the default index.

QueryMatchRatio create(OpsScorerInitParams params, CString indexName)

Creates a QueryMatchRatio object that calculates the ratio of terms that a search query hits in all fields of a specified index to all terms in the specified index.

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

Creates a QueryMatchRatio object that calculates the ratio of terms that a search query hits in a specified field of a specified index to all terms in the specified index.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the QueryMatchRatio 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 specified field or all fields of an index to all terms in the index.

Function details

QueryMatchRatio(OpsScorerInitParams params)

Creates a QueryMatchRatio object that calculates the ratio of terms that a search query hits in all fields of the default index to all terms in the default index. Parameter: params: the parameters that are used for initialization. For more information, see OpsScorerInitParams.

QueryMatchRatio(OpsScorerInitParams params, CString indexName)

Creates a QueryMatchRatio object that calculates the ratio of terms that a search query hits in all fields of a specified index to all terms in the specified index. 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, the analyzer for English, or the analyzer for fuzzy searches.

QueryMatchRatio(OpsScorerInitParams params, CString indexName, CString fieldName)

Creates a QueryMatchRatio object that calculates the ratio of terms that a search query hits in a specified field of a specified index to all terms in the specified index. 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, the analyzer for English, or the analyzer for fuzzy searches. fieldName: the name of a field of the index that is specified by the indexName parameter. The name must be a constant.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the QueryMatchRatio scores 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 QueryMatchRatio scores of multiple query groups. Supported aggregation methods are max and sum.

double evaluate(OpsScoreParams params)

Calculates the ratio of terms that a search query hits in a specified field or all fields of an index to all terms in the index. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: the ratio of terms that the search query hits in the specified field or all fields of the index to all terms in the index. Valid values: [0,1]. Sample code:

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.similarity.querymatch.QueryMatchRatio;

class BasicSimilarityScorer {
    QueryMatchRatio _f1;
    QueryMatchRatio _f2;
    boolean init(OpsScorerInitParams params) {
        _f1 = QueryMatchRatio.create(params, "title_index");
        _f2 = QueryMatchRatio.create(params);
        return true;
    }

    double score(OpsScoreParams params) {
        return _f1.evaluate(params) + _f2.evaluate(params);
    }
}