All Products
Search
Document Center

:OpsKvPairs

Last Updated:Feb 06, 2023

Overview

The OpsKvPairs class is used to obtain custom key-value pairs. For more information about custom key-value pairs, see kvpairs clause. The OpsKvPairs class also provides a set of functions that allow you to process the values of custom parameters. For example, you can convert the values of custom parameters to arrays.

Functions

Function

Description

CString getValue(CString key)

Obtains the value of a custom parameter of the CString type.

long getLong(CString key)

Obtains the value of a custom parameter of the CString type and converts the value to a LONG value.

float getFloat(CString key)

Obtains the value of a custom parameter of the CString type and converts the value to a FLOAT value.

double getDouble(CString key)

Obtains the value of a custom parameter of the CString type and converts the value to a DOUBLE value.

long[] getLongArray(CString key, CString sep)

Obtains the values of specified custom parameters of the CString type and converts the values to an array of the LONG type.

float[] getFloatArray(CString key, CString sep)

Obtains the values of specified custom parameters of the CString type and converts the values to an array of the FLOAT type.

double[] getDoubleArray(CString key, CString sep)

Obtains the values of specified custom parameters of the CString type and converts the values to an array of the DOUBLE type.

Function details

CString getValue(CString key)

Obtains the value of a custom parameter of the CString type. The custom parameter must be defined in the kvpairs clause. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the value of the custom parameter. If the specified key does not exist, the return value is null.

Sample code:

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 {
    CString flag;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        flag = kvpairs.getValue("flag"); // Obtain the value of the custom parameter whose key is flag from the kvpairs clause.
        return true;
    }

    double score(OpsScoreParams params) {
        if (flag != null && flag.equals("abc")) {
            //do something
        }
        return 0.0;
    }      
}

long getLong(CString key)

Obtains the value of a custom parameter of the CString type and converts the value to a LONG value. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the LONG value that is converted from the value of the custom parameter. If the specified key does not exist or the value of the custom parameter cannot be converted to a LONG value, the return value is 0.

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;
    }      
}

float getFloat(CString key)

Obtains the value of a custom parameter of the CString type and converts the value to a FLOAT value. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the FLOAT value that is converted from the value of the custom parameter. If the specified key does not exist or the value of the custom parameter cannot be converted to a FLOAT value, the return value is 0.

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 {
    float ratio;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        ratio = kvparis.getFloat("ratio"); // Obtain the value of the custom parameter whose key is flag from the kvpairs clause, and convert the value to a FLOAT value.
        return true;
    }

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

double getDouble(CString key)

Obtains the value of a custom parameter of the CString type and converts the value to a DOUBLE value. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the DOUBLE value that is converted from the value of the custom parameter. If the specified key does not exist or the value of the custom parameter cannot be converted to a DOUBLE value, the return value is 0.

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 {
    double ratio;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        ratio = kvparis.getDouble("ratio"); // Obtain the value of the custom parameter whose key is flag from the kvpairs clause, and convert the value to a DOUBLE value.
        return true;
    }

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

long[] getLongArray(CString key, CString sep)

Obtains the values of specified custom parameters of the CString type and converts the values to an array of the LONG type. The converted values are separated by the specified delimiter. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the array of the LONG type that is converted from the values of the specified custom parameters. If the specified key does not exist or the values of the custom parameters cannot be converted to LONG values, the return value is null.

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.OpsDoc;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsKvPairs;
class BasicSimilarityScorer {
    long[] flags;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        flags = kvparis.getLongArray("flags", ";"); 
        params.getDoc().requireAttribute("flag");
        return true;
    }

    double score(OpsScoreParams params) {
        long docFlag = params.getDoc().docFieldLong("flag");
        if (flag != null && flags.length > 0 && docFlag == flags[0]) {
            //do something
        }
        return 0.0;
    }      
}

float[] getFloatArray(CString key, CString sep)

Obtains the values of specified custom parameters of the CString type and converts the values to an array of the FLOAT type. The converted values are separated by the specified delimiter. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the array of the FLOAT type that is converted from the values of the specified custom parameters. If the specified key does not exist or the values of the custom parameters cannot be converted to FLOAT values, the return value is null.

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.OpsDoc;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsKvPairs;
class BasicSimilarityScorer {
    float[] weights;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        weights = kvparis.getFloatArray("weights", ";"); 
        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;
    }      
}

double[] getDoubleArray(CString key, CString sep)

Obtains the values of specified custom parameters of the CString type and converts the values to an array of the DOUBLE type. The converted values are separated by the specified delimiter. Parameter: key: the key of the custom parameter that you want to obtain. Return value: the array of the DOUBLE type that is converted from the values of the specified custom parameters. If the specified key does not exist or the values of the custom parameters cannot be converted to DOUBLE values, the return value is null.

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.OpsDoc;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsKvPairs;
class BasicSimilarityScorer {
    double[] weights;
    boolean init(OpsScorerInitParams params) {
        OpsRequest request = params.getRequest();
        OpsKvPairs kvparis = request.getKVPairs();
        weights = kvparis.getDoubleArray("weights", ";"); 
        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;
    }      
}