All Products
Search
Document Center

OpenSearch:TextRelevance

Last Updated:Feb 06, 2023

Overview

The TextRelevance class is used to calculate the text relevance between a search query and the specified field in the specified index. The TextRelevance class calculates the text relevance based on the following metrics: the proportion of hit terms in the search query, the proportion of hit terms in the field, the frequency of hit terms in the field, the order of hit terms in the field, and the order of hit terms in the search query. The larger the return value is, the more the search query is relevant to the specified field. The implementation of the TextRelevance class involves the implementation of a FieldMatchWeighted object, a BM25 object, and a FieldTermProximity object. You can use the functions that are provided for the TextRelevance class to adjust the weight of these three types of objects. This way, you can customize the TextRelevance class.

Functions

Function

Description

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

Creates a TextRelevance object.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the text relevance scores of multiple query groups. Supported aggregation methods are sum and max. The default aggregation method is sum.

void setFieldBm25Weight(double weight)

Sets the weight of BM25 scores.

void setFieldMatchWeightedWeight(double weight)

Sets the weight of FieldMatchWeighted scores.

void setFieldTermProximityWeight(double weight)

Sets the weight of FieldTermProximity scores.

double evaluate(OpsScoreParams params)

Calculates the text relevance between a search query and the specified field in the specified index.

Function details

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

Creates a TextRelevance object, which is used to calculate the text relevance between a search query and the specified field in the specified index. Parameters: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. 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 text relevance 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 text relevance scores of multiple query groups. Supported aggregation methods are max and sum.

void setFieldBm25Weight(double weight)

Sets the weight of BM25 scores. This function can be called only during the initialization of a score calculation object. The default weight is 0.5. Parameter: weight: the weight of BM25 scores.

void setFieldMatchWeightedWeight(double weight)

Sets the weight of MatchWeighted scores. This function can be called only during the initialization of a score calculation object. The default weight is 5. Parameter: weight: the weight of BM25 scores.

void setFieldTermProximityWeight(double weight)

Sets the weight of TermProximity scores. This function can be called only during the initialization of a score calculation object. The default weight is 9. Parameter: weight: the weight of BM25 scores.

double evaluate(OpsScoreParams params)

Calculates the text relevance between a search query and the specified field in the specified index. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: The text relevance between the search query and the specified field in the specified 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.TextRelevance;

class BasicSimilarityScorer {
    TextRelevance _f1;
    boolean init(OpsScorerInitParams params) {
        _f1 = TextRelevance.create(params, "text_index", "text");
        _f1.setGroupScoreMergeOp("max");
        _f1.setFieldBm25Weight(2);
        _f1.setFieldMatchWeightedWeight(3);
        _f1.setFieldTermProximityWeight(4);
        return true;
    }

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