All Products
Search
Document Center

OpenSearch:FieldTermProximity

Last Updated:Feb 06, 2023

Overview

The FieldTermProximity class is used to calculate the proximity of a term to the specified field in the specified index. The calculated proximity indicates the relevance of the term to the field. For example, the term is User Manual. The value of Field 1 is OpenSearch User Manual. The value of Field 2 is Users use OpenSearch Manual to solve problems. The calculated proximity of the term to Field 1 is greater than that of the term to Field 2. This means that the term is more relevant to Field 1 than to Field 2. The calculated proximity also indicates whether the term is integral in the field.

Functions

Function

Description

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

Creates a FieldTermProximity object.

void setGroupScoreMergeOp(CString opName)

Sets an aggregation method for the FieldTermProximity 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 the query term in the specified field.

Function details

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

Creates a FieldTermProximity object based on the specified index and the specified field. Parameter: 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 FieldTermProximity scores of multiple query groups. This function must be invoked in the init function. Parameter: opName: the name of an operator. Supported operators are sum and max. The sum operator calculates a sum of multiple scores. The max operator returns the maximum value among multiple scores. The default value is sum.

double evaluate(OpsScoreParams params)

Calculates the proximity of the query term in the specified field. Parameter: params: the parameters that are used for score calculation. For more information, see OpsScoreParams. Return value: the FieldTermProximity score of the term in the specified field. 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.distribution.FieldTermProximity;

class BasicSimilarityScorer {
    FieldTermProximity _proximity;
    boolean init(OpsScorerInitParams params) {
        _proximity = FieldTermProximity.create(params, "text_index","text");
        _proximity.setGroupScoreMergeOp("max");
        return true;
    }

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