OpsKvPairs reads custom key-value pairs from the kvpairs clause of an incoming query. It also converts string values to numeric types or arrays, so your scorer can work directly with typed data.
Retrieve an OpsKvPairs instance in your scorer's init() method:
OpsKvPairs kvpairs = params.getRequest().getKVPairs();Functions
| Function | Description | Default when key is missing |
|---|---|---|
CString getValue(CString key) | Returns the value of a custom parameter as a CString. | null |
long getLong(CString key) | Returns the value of a custom parameter converted to a long. | 0 |
float getFloat(CString key) | Returns the value of a custom parameter converted to a float. | 0 |
double getDouble(CString key) | Returns the value of a custom parameter converted to a double. | 0 |
long[] getLongArray(CString key, CString sep) | Splits the value of a custom parameter by the delimiter and returns a long[]. | null |
float[] getFloatArray(CString key, CString sep) | Splits the value of a custom parameter by the delimiter and returns a float[]. | null |
double[] getDoubleArray(CString key, CString sep) | Splits the value of a custom parameter by the delimiter and returns a double[]. | null |
Function details
All examples use the same scorer skeleton. The full class structure is shown once below; subsequent examples show only the relevant lines.
package users.scorer;
import cava.lang.CString;
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 {
// field declarations and init() / score() shown in each example below
}getValue(CString key)
Returns the value of a custom parameter as a CString. The key must be defined in the kvpairs clause.
Parameters: key — the key of the custom parameter to retrieve.
Returns: The parameter value as a CString. Returns null if the key does not exist.
class BasicSimilarityScorer {
CString flag;
boolean init(OpsScorerInitParams params) {
OpsKvPairs kvpairs = params.getRequest().getKVPairs();
flag = kvpairs.getValue("flag"); // null if "flag" is not in the kvpairs clause
return true;
}
double score(OpsScoreParams params) {
if (flag != null && flag.equals("abc")) {
// do something
}
return 0.0;
}
}getLong(CString key)
Returns the value of a custom parameter converted to a long.
Parameters: key — the key of the custom parameter to retrieve.
Returns: The parameter value as a long. Returns 0 if the key does not exist or the value cannot be converted to a long.
// In init():
long flag = kvpairs.getLong("flag"); // 0 if "flag" is absent or non-numeric
// In score():
if (flag == 1) {
// do something
}getFloat(CString key)
Returns the value of a custom parameter converted to a float.
Parameters: key — the key of the custom parameter to retrieve.
Returns: The parameter value as a float. Returns 0 if the key does not exist or the value cannot be converted to a float.
// In init():
float ratio = kvpairs.getFloat("ratio"); // 0 if "ratio" is absent or non-numeric
// In score():
if (ratio > 1) {
// do something
}getDouble(CString key)
Returns the value of a custom parameter converted to a double.
Parameters: key — the key of the custom parameter to retrieve.
Returns: The parameter value as a double. Returns 0 if the key does not exist or the value cannot be converted to a double.
// In init():
double ratio = kvpairs.getDouble("ratio"); // 0 if "ratio" is absent or non-numeric
// In score():
if (ratio > 1) {
// do something
}getLongArray(CString key, CString sep)
Splits the value of a custom parameter by the specified delimiter and returns the parts as a long[].
Parameters:
key— the key of the custom parameter to retrieve.sep— the delimiter used to split the parameter value.
Returns: A long[] of the split and converted values. Returns null if the key does not exist or the values cannot be converted to long.
import com.aliyun.opensearch.cava.framework.OpsDoc;
class BasicSimilarityScorer {
long[] flags;
boolean init(OpsScorerInitParams params) {
OpsKvPairs kvpairs = params.getRequest().getKVPairs();
flags = kvpairs.getLongArray("flags", ";"); // splits "1;2;3" into [1, 2, 3]
params.getDoc().requireAttribute("flag");
return true;
}
double score(OpsScoreParams params) {
long docFlag = params.getDoc().docFieldLong("flag");
if (flags != null && flags.length > 0 && docFlag == flags[0]) {
// do something
}
return 0.0;
}
}getFloatArray(CString key, CString sep)
Splits the value of a custom parameter by the specified delimiter and returns the parts as a float[].
Parameters:
key— the key of the custom parameter to retrieve.sep— the delimiter used to split the parameter value.
Returns: A float[] of the split and converted values. Returns null if the key does not exist or the values cannot be converted to float.
import com.aliyun.opensearch.cava.framework.OpsDoc;
class BasicSimilarityScorer {
float[] weights;
boolean init(OpsScorerInitParams params) {
OpsKvPairs kvpairs = params.getRequest().getKVPairs();
weights = kvpairs.getFloatArray("weights", ";"); // splits "0.5;0.3;0.2" into [0.5, 0.3, 0.2]
params.getDoc().requireAttribute("flags");
return true;
}
double score(OpsScoreParams params) {
long[] docFlags = params.getDoc().docFieldLongArray("flags");
if (docFlags == null || weights == null) {
return 0.0;
}
double score = 0.0;
if (weights.length == docFlags.length) {
for (int i = 0; i < weights.length; ++i) {
score += docFlags[i] * weights[i];
}
}
return score;
}
}getDoubleArray(CString key, CString sep)
Splits the value of a custom parameter by the specified delimiter and returns the parts as a double[].
Parameters:
key— the key of the custom parameter to retrieve.sep— the delimiter used to split the parameter value.
Returns: A double[] of the split and converted values. Returns null if the key does not exist or the values cannot be converted to double.
import com.aliyun.opensearch.cava.framework.OpsDoc;
class BasicSimilarityScorer {
double[] weights;
boolean init(OpsScorerInitParams params) {
OpsKvPairs kvpairs = params.getRequest().getKVPairs();
weights = kvpairs.getDoubleArray("weights", ";"); // splits "0.5;0.3;0.2" into [0.5, 0.3, 0.2]
params.getDoc().requireAttribute("flags");
return true;
}
double score(OpsScoreParams params) {
long[] docFlags = params.getDoc().docFieldLongArray("flags");
if (docFlags == null || weights == null) {
return 0.0;
}
double score = 0.0;
if (weights.length == docFlags.length) {
for (int i = 0; i < weights.length; ++i) {
score += docFlags[i] * weights[i];
}
}
return score;
}
}