All Products
Search
Document Center

OpenSearch:OpsRequest

Last Updated:Apr 01, 2026

OpsRequest represents a query request in the Cava scoring framework. Use it to access the custom parameters passed in the kvpairs clause of a query. OpsRequest exposes a single function: getKVPairs().

Function

FunctionDescription
OpsKvPairs getKVPairs()Returns all custom parameters as key-value pairs.

Function details

getKVPairs()

OpsKvPairs getKVPairs()

Returns an OpsKvPairs object containing all custom parameters defined in the kvpairs clause of a query.

Call this method in init(), not in score().

The score() function runs once for every document involved in scoring. Calling getKVPairs() inside score() adds overhead on each invocation. Calling it in init() instead runs the lookup exactly once per query.

Example

The following example retrieves the flag parameter during scorer initialization and uses it in the score function.

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 kvpairs = request.getKVPairs();
        flag = kvpairs.getLong("flag"); // Get the value of the "flag" key from the kvpairs clause as a long value.
        return true;
    }

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