I find a function in ElasticSearch like
GET /_search
{
"function_score": {
"functions": [
{ ...location clause... },
{ ...price clause... },
{
"script_score": {
"params": {
"threshold": 80,
"discount": 0.1,
"target": 10
},
"script": "price = doc['price'].value; margin = doc['margin'].value;
if (price < threshold) { return price * margin / target };
return price * (1 - discount) * margin / target;"
}
}
]
}
}
- I use
ScoreFunctionBuilderto achive the "location caluse" and "price caluse" ,but I do not know how to write"script_score"and"script"by java api - The ES version in my project is 2.2.0 and I use the java api to achieve this function
- But I can not find the API like
ScriptScoreFunctionBuilder. scriptFunction(String script, Map<String, Object> params)how can I do? Thx :) - Finally I figure out how to write it by using JD-JUI to check the "elasticsearch.jar" file.
EX:
Map<String, Object> params = new HashMap<>();
params.put("num1", 10);
params.put("num2", 4);
String inlineScript = "doc['score'].value * num1 * num2";
Script script = new Script(script, ScriptType.INLINE, "groovy", params);
ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(ss);
that's all