All Products
Search
Document Center

OpsKvPairs

Last Updated: Sep 09, 2021

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 a specified custom parameter of the CString type.

long getLong(CString key)

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

float getFloat(CString key)

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

double getDouble(CString key)

Obtains the value of a specified 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 a specified custom parameter of the CString type. The custom parameter must be in the kvpairs clause. Parameter: key: the key of the custom parameter that you want to obtain. Return value: 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 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 specified 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 specified 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 specified 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. Parameter: key or sep: 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 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. Parameter: key or sep: 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 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. Parameter: key or sep: 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 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;
    }      
}