All Products
Search
Document Center

:OpsRequest

Last Updated:Feb 20, 2023

Overview

The OpsRequest class is used to define a query request. This class can help you obtain information from the query request. The OpsRequest class supports only the OpsKvPairs getKVPairs() function, which allows you to obtain an OpsKvPairs object. If custom parameters are defined in the kvpairs clause in a query, the OpsRequest class can be used to obtain the custom parameters during score calculation. We recommend that you use the OpsRequest class to obtain custom parameters during the initialization instead of score calculation of a score calculation object. The score function is invoked for each document that is involved in score calculation. If you obtain custom request parameters during score calculation, calculation costs are increased.

Function

Function

Description

OpsKvPairs getKVPairs()

Obtains all the key-value pairs.

Function details

OpsKvPairs getKVPairs()

Obtains all the custom parameters in the form of key-value pairs. For more information about the OpsKvPairs class, see OpsKvPairs. 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.OpsKvPairs;
class BasicSimilarityScorer {
    long flag;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        flag = kvpairs.getLong("flag"); // Obtain the value of the custom parameter whose key is flag from the kvpairs clause, and convert the value to a LONG value.
        return true;
    }

    double score(OpsScoreParams params) {
        if (flag == 1) {
            //do something
        }
        return 0.0;
    }      
}