All Products
Search
Document Center

OpenSearch:KeyWordsMatched

Last Updated:Feb 20, 2023

Overview

The KeyWordsMatched class is used to measure the proximity of the keywords of a search query to a specific field. If the terms after analysis of the search query do not appear in the field, 0 is returned. If the keywords of the search query appear in the field, 0.5 is returned. If all the terms after analysis of the search query appear in the field, 1.0 is returned. For example, a search query is A AND B RANK C. If none of the terms A, B, or C appears in the specified field, the return value is 0. If only the terms A and B appear in the field, the return value is 0.5. If all the terms A, B, and C appear in the field, the return value is 1.0. The keywords of a search query are determined in two ways. If an analyzer is used, the keywords are determined during score calculation based on the output information of the analyzer. Otherwise, the keywords are determined based on the logic of term combination in the search query.

Functions

Function

Description

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

Creates a KeyWordsMatched object. This function is a factory function.

void setGroupScoreMergeOp(CString opName)

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

double evaluate(OpsScoreParams params)

Calculates the proximity of a query term to a specific field.

Function details

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

Creates a KeyWordsMatched object. You must specify an index and a field to be matched. 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. 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 KeyWordsMatched 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. Parameter: opName: the method that is used to aggregate the KeyWordsMatched scores of multiple query groups. Supported aggregation methods are max and sum.

double evaluate(OpsScoreParams params)

Calculates the proximity of the keywords of a search query to a specific field. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: the proximity of the keywords of a search query to a specific field. Valid values: 0, 0.5, and 1.0. 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.fieldmatch.KeyWordsMatched;

class BasicSimilarityScorer {
    KeyWordsMatched _f1;
    boolean init(OpsScorerInitParams params) {
        _f1 = KeyWordsMatched.create(params, "pack_index1", "text_field");
        return true;
    }

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