我正在尝试使用以下脚本进行排序:
if (doc['stats.favoriteCount'].value > doc['stats.likeCount'].value)
{
return doc['stats.favoriteCount'].value;
}
return doc['stats.likeCount'].value;
这是我正在使用的请求:
{
"sort": [
{
"_script": {
"script": {
"file": "sorting-likes",
"lang": "groovy"
},
"type": "number"
}
}
]
}
这就是我得到的错误:
"reason": {
"type": "groovy_script_execution_exception",
"reason": "failed to run file script [sorting-likes] using lang [groovy]",
"caused_by": {
"type": "missing_method_exception",
"reason": "No signature of method: 7b6bb56cf8fda7e1301b150b05d81813258c223b.if() is applicable for argument types: (java.lang.Boolean, 7b6bb56cf8fda7e1301b150b05d81813258c223b$_run_closure1) values: [true, 7b6bb56cf8fda7e1301b150b05d81813258c223b$_run_closure1@63c6a777]\nPossible solutions: wait(), run(), run(), any(), find(), is(java.lang.Object)"
}
}
我真的看不出什么错。这曾经是一个更长的脚本,我把它撕成了我可以使用的最小值。
以内联方式运行此代码效果很好:
{
"sort": [
{
"_script": {
"script": {
"inline": "if (doc['stats.favoriteCount'].value>doc['stats.likeCount'].value)return doc['stats.favoriteCount'].value;return doc['stats.likeCount'].value;",
"lang": "groovy"
},
"type": "number"
}
}
]
}
由于
答案 0 :(得分:0)
仍然不知道为什么原始剧本不起作用,但这很有效:
<强>查询:强>
"sort": [
{
"_script": {
"script": {
"file": "sorting",
"params": {
"param0": "stats.likeCount",
"param1": "stats.favoriteCount",
"param2": "",
"param3": ""
},
"lang": "groovy"
},
"type": "number",
"order": "asc",
"missing": 0
}
}
]
<强>脚本强>
if (!param2)
return max(doc[param0].value, doc[param1].value);
if (!param3)
return max(doc[param0].value, max(doc[param1].value, doc[param2].value));
return max(doc[param0].value, max(doc[param1].value, max(doc[param2].value, doc[param3].value)));