不能对[org.elasticsearch.index.fielddata.ScriptDocValues]类型应用[*



这是索引属性的映射-"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.factorjava.lang.Double型。

所以你得到的错误意味着一些文档在你的hoi字段中有多个值。因此,您需要使用doc['hoi'].value * params.factor来引用它,它将从数组中获取第一个hoi值来计算排序。

您还可以使用doc['hoi'].size()检查有多少个值。

如果有些文档没有hoi的值,那么你可以这样调整脚本:

doc['hoi'].size() > 0 ? doc['hoi'].value * params.factor : 0

相关内容

  • 没有找到相关文章

最新更新