gpt4 book ai didi

api - 如何在java api中编写elasticsearch script_score

转载 作者:行者123 更新时间:2023-12-01 10:12:41 28 4
gpt4 key购买 nike

我在 ElasticSearch 中找到了一个类似的函数

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;"
}
}
]
}
}
  • 我使用ScoreFunctionBuilder实现“位置计算”并且“价格计算”,但我不知道怎么写"script_score""script"通过java API
  • 我的项目中的ES版本是2.2.0,我使用的是java api实现该功能
  • 但是我找不到像这样的API ScriptScoreFunctionBuilder. scriptFunction(String script,
    Map<String, Object> params)
    我该怎么办?谢谢:)
  • 最后我通过使用JD-JUI检查“elasticsearch.jar”文件弄清楚了如何编写它。

例如:

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

仅此而已

最佳答案

这是如何在java中实现scrip_score函数

Map<String, Object> params = new HashMap<>();                   
params.put("paramName1", "paramVal1")
params.put("paramName2", "paramVal2");

String script = "price = doc['price'].value; margin = doc['margin'].value;
if (price < threshold) { return price * margin / target };
return price * (1 - discount) * margin / target;"
XContentBuilder builder =
new ScriptScoreFunctionBuilder()
.script(script)
.lang("groovy")
.params(params)
.toXContent(XContentBuilder, params);

关于api - 如何在java api中编写elasticsearch script_score,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36054977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com