无痛无法访问对象内的数组

Painless Unable to access Array within an object

我试图在无痛脚本中访问源中的数组对象,但我无法访问该错误

"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"
          }
        }
      }
    ]

我的要求是

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

对象存储为

“_来源”:{ “boxArray”:[{ "boxName": "红色" }] }

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

Doc-values can only return "simple" field values like numbers, dates, geo- points, terms, etc, or arrays of these values if the field is multi-valued. It cannot return JSON objects.

有没有办法在无痛脚本中访问 json 对象和数组?我在这里遗漏了什么吗?

脚本查询只能访问文档值,因此如果您将其更改为此脚本,您的脚本将起作用。

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

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