FieldLength

更新时间:
复制 MD 格式

Returns the number of terms produced when an analyzer processes a field in an index.

Functions

SignatureDescription
FieldLength create(OpsScorerInitParams params, CString indexName, CString fieldName)Creates a FieldLength object bound to a specific index and field.
double evaluate(OpsScoreParams params)Returns the term count for the field in the current document.

Function details

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

Creates a FieldLength object and binds it to a specific field in a specific index.

Parameters

ParameterTypeDescription
paramsOpsScorerInitParamsScoring initialization parameters. See OpsScoreParams.
indexNameCStringThe name of the index. Must be a constant — dynamic values are not supported.
fieldNameCStringThe name of the field in the specified index. Must be a constant. The field must be of type TEXT or SHORT_TEXT and must use one of the following analyzers: general analyzer for Chinese, custom analyzer, single character analyzer for Chinese, analyzer for English, or analyzer for fuzzy searches.

double evaluate(OpsScoreParams params)

Returns the number of terms in the field after analysis for the current document.

Parameters

ParameterTypeDescription
paramsOpsScoreParamsScoring parameters for the current query. See OpsScoreParams.

Example

The following scorer returns the field length of text_field in text_index1 as the document score.

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.fieldmatch.FieldLength;

class BasicSimilarityScorer {
    FieldLength _fieldLength;

    boolean init(OpsScorerInitParams params) {
        // Bind to a specific index and field — both arguments must be constants
        _fieldLength = FieldLength.create(params, "text_index1", "text_field");
        return true;
    }

    double score(OpsScoreParams params) {
        // Returns the number of terms in text_field after analysis
        return _fieldLength.evaluate(params);
    }
}