OpsKvPairs

简介

OpsKvPairs用户获取用户自定义的kvpairs信息,关于如果自定义kvpair,请参考自定义kvpairs子句。在获取用户自定义参数过程中,OpsKvPairs还提供了一些函数,方便用户对定义的参数做进一步的处理,比如讲定义的参数转换成特定数据类型的数组。

函数列表

函数原型

函数简介

CString getValue(CString key)

获取CString类型的参数

long getLong(CString key)

获取long类型的参数

float getFloat(CString key)

获取float类型的参数

double getDouble(CString key)

获取double类型的参数

long[] getLongArray(CString key, CString sep)

获取自定义参数,并返回一个长整型数组

float[] getFloatArray(CString key, CString sep)

获取CString类型的参数

double[] getDoubleArray(CString key, CString sep)

获取CString类型的参数

函数详情

CString getValue(CString key)

从kvpairs中获取CString类型的参数。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在返回null。

代码示例:

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 = kvparis.getValue("flag"); //假设kvpairs中有一个CString类型的flag变量
        return true;
    }

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

long getLong(CString key)

从kvpairs中获取long类型的参数。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在或者无法转成long类型返回0。

代码示例:

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 = kvparis.getLong("flag"); //假设kvpairs中有一个长整型的flag变量
        return true;
    }

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

float getFloat(CString key)

从kvpairs中获取float类型的参数。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在或者无法转成float类型返回0。

代码示例:

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"); //假设kvpairs中有一个float类型的ratio变量
        return true;
    }

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

double getDouble(CString key)

从kvpairs中获取double类型的参数。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在或者无法转成double类型返回0。

代码示例:

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"); //假设kvpairs中有一个double类型的ratio变量
        return true;
    }

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

long[] getLongArray(CString key, CString sep)

获取kvpairs中的参数,参数的值为按照指定分隔符分隔的long型数组。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在或者无法转成long类型返回null。

代码示例:

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)

获取kvpairs中的参数,参数的值为按照指定分隔符分隔的float型数组。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在或者无法转成float类型返回null。

代码示例:

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)

获取kvpairs中的参数,参数的值为按照指定分隔符分隔的double型数组。参数列表:key — 参数在kvpairs中的key返回值:返回自定义的参数,如果key不存在或者无法转成double类型返回null。

代码示例:

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