elasticsearch无痛脚本将对象字段添加到文档中

kyks70gy  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(402)

我有一个相当基本的要求,我想添加一个对象到一个无痛的查询内更新文档。
这就是我想补充的对象

"memoire": {
      "codeActe": "C",
      "docType": "M",
      "uuid": "added ",
      "nonFacturable": false,
      "published": false
    },

我试过了

POST /csa_encounters/_update_by_query
{
  "script":
{ "source": """

  ctx._source.memoire.codeActe = "C";
  ctx._source.memoire.docType= "M";
  ctx._source.memoire.uuid = "added ";
  ctx._source.memoire.nonFacturable = false;
  ctx._source.memoire.published =false
""",
"lang": "painless"
},
    "query": {
        "bool" : {
            "must" : [
                { "query_string" : { "query" : "140c7646", "fields" : [ "_id" ] } }
            ]
        }
    }
}

但是我得到消息null指针异常,因为这个字段当然不存在

"type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.memoire.codeActe = \"C\";\n  ",
      "                   ^---- HERE"
    ],
 ...
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 400

如果对象还不存在,我就不知道如何将其创建为新字段。

g2ieeal7

g2ieeal71#

因为这片土地 memoire 文档中不存在。因此,只需在脚本开头添加以下行:

if (ctx._source.memoire == null) {
    ctx._source.memoire = [:];
}

相关问题