更新嵌套字符串字段

时间:2018-06-21 12:38:59

标签: elasticsearch nosql elasticsearch-painless

我正在尝试通过image.uri更新字段_update_by_query

POST user/_update_by_query
{
  "script": {
    "source": "ctx._source.image.uri = 'https://example.com/default/image/profile.jpg'",
    "lang": "painless"
  },
  "query": {
    "bool": {
      "must_not": [
         {
              "exists": {
                "field": "image.id"
              }
          }
      ]
    }
  }
}

但是会引发错误:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.image.uri = 'https://example.com/default/image/profile.jpg'",
          "                 ^---- HERE"
        ],
        "script": "ctx._source.image.uri = 'https://example.com/default/image/profile.jpg'",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.image.uri = 'https://example.com/default/image/profile.jpg'",
      "                 ^---- HERE"
    ],
    "script": "ctx._source.image.uri = 'https://example.com/default/image/profile.jpg'",
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 500
}

示例文档:

{
    "image": {
        "uri": "https://example.com/resources/uploads/default_files/profile/thumb/large/default_profile.jpg"
    },
    "created": "2018-06-06T21:49:26Z",
    "uid": 1,
    "name": "Jason Cameron",
    "username": "jason"
}

1 个答案:

答案 0 :(得分:0)

更新的响应

问题可能来自文档中没有image对象的文档。

如果可能,请尝试添加strict映射,以避免索引没有image对象的文档。

OLD RESPONSE /“ \'对于在无痛脚本中作为字符串使用是正确的

您的问题来自使用'封装您的uri,字符串必须由"封装。

尝试将脚本修改为:

 "script": {
    "source": "ctx._source.image.uri = \"https://example.com/default/image/profile.jpg\"",
    "lang": "painless"
  }