这是索引属性的映射-"hoi": {"type":";float"}
这是我想要执行的查询。我想用动态值改变我的属性hoi并在其上对文档进行排序,不想使用运行时映射,因为我有较低版本的ES-
GET test/_search
{
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"lang": "painless",
"source": "doc['hoi'] * params.factor",
"params" : {
"factor" : 1.1
}
},
"order" : "asc"
}
}
}
错误——
{
"error" : {
"root_cause" : [
{
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"doc['hoi'] * params.factor",
" ^---- HERE"
],
"script" : "doc['hoi'] * params.factor",
"lang" : "painless",
"position" : {
"offset" : 40,
"start" : 0,
"end" : 47
}
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "test",
"node" : "5sfrbDiLQDOg82_sneaU1g",
"reason" : {
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"doc['hoi'] * params.factor",
" ^---- HERE"
],
"script" : "doc['hoi'] * params.factor",
"lang" : "painless",
"position" : {
"offset" : 40,
"start" : 0,
"end" : 47
},
"caused_by" : {
"type" : "class_cast_exception",
"reason" : "Cannot apply [*] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Doubles] and [java.lang.Double]."
}
}
}
]
},
"status" : 400
}
如何让它工作?
doc['hoi']
本身为org.elasticsearch.index.fielddata.ScriptDocValues.Doubles
型,params.factor
为java.lang.Double
型。
所以你得到的错误意味着一些文档在你的hoi
字段中有多个值。因此,您需要使用doc['hoi'].value * params.factor
来引用它,它将从数组中获取第一个hoi
值来计算排序。
您还可以使用doc['hoi'].size()
检查有多少个值。
如果有些文档没有hoi
的值,那么你可以这样调整脚本:
doc['hoi'].size() > 0 ? doc['hoi'].value * params.factor : 0