Painless无法访问对象中的Array



我试图在源代码中访问一个数组对象,但我不能与错误

"failed_shards" : [
{
"shard" : 0,
"index" : "boxIndex",
"node" : "gsbkERMmT72w3IV4SwuIDw",
"reason" : {
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"if (params._source.boxArray[0].boxName =="red"){n                        ",
"                  ^---- HERE"
],
"script" : " ...",
"lang" : "painless",
"position" : {
"offset" : 41,
"start" : 23,
"end" : 105
},
"caused_by" : {
"type" : "null_pointer_exception",
"reason" : "Cannot invoke "Object.getClass()" because "callArgs[0]" is null"
}
}
}
]

My Request is

GET /boxIndex/_search
{
"query":{
"bool":{
"must":{
"script":{
"script":{
"lang":"painless",
"source": """
if (params._source.boxArray[0].boxName == "red"){
return true;
}
return false;
"""
}
}
}
}
}
}

对象存储为

"_source" {"boxArray" [{"boxName"red"})}

我不能弄清楚,因为ES文档似乎在说它应该工作:https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html。我不能像文档中所说的那样使用doc对象:

Doc-values只能返回"simple"字段值,比如数字、日期、地理点、项等,或这些值的数组(如果字段是)多值。它不能返回JSON对象。

是否有一种方法来访问json对象和数组内的无痛脚本?我遗漏了什么吗?

一个脚本查询只能访问文档值,所以如果你把它改成这样,你的脚本就可以工作了。

if (doc['boxArray.boxName.keyword'].get(0) == "red"){
return true;
}
return false;

但是,在非嵌套数据的数组中,文档值列表的顺序可能与源文档中的顺序不同,因此这里可能会有所不同。

最新更新