0

I am trying to update a field image.uri by _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"
              }
          }
      ]
    }
  }
}

But it throws error:

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

A sample document:

{
    "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"
}
Shaharyar
  • 12,254
  • 4
  • 46
  • 66

1 Answers1

0

UPDATED RESPONE

The problem could be coming from a document without image object in it.

Try to add strict mapping if possible, to avoid indexing documents without image object.

OLD RESPONSE/"\' are correct for use inside painless script as string

Your problem comes as use of ' to encapsulate your uri, strings must be encapsulated by ".

Try to modify your script as:

 "script": {
    "source": "ctx._source.image.uri = \"https://example.com/default/image/profile.jpg\"",
    "lang": "painless"
  }
jordivador
  • 1,016
  • 11
  • 26
  • Hm I've tried it, and worked as expected with mapping with `object.id`. Should you share your `image` attributes mapping? – jordivador Jun 21 '18 at 12:50
  • It updates first level keys like `_source.name`, my query worked for it. But its not updating this nested field. Here is the mapping: ` "image": { "properties": { "id": { "type": "long" }, "uri": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }` – Shaharyar Jun 21 '18 at 12:52
  • What ES version are you using? I've tried your script with ES 6 and your image mapping with `"query":{"match_all":{}}` and it worked correctly. – jordivador Jun 21 '18 at 12:58
  • This worked for me: https://gist.github.com/jordivador/eaa38fe60bbf83235ec2d0af83ab2112 – jordivador Jun 21 '18 at 13:03
  • I've found your possible error, try to found any document that has no `image` attribute at all. – jordivador Jun 21 '18 at 13:32
  • I did, no documents found. I am trying to identify the issue. How can I check in `painless` if its null? – Shaharyar Jun 21 '18 at 13:57
  • Elvis operator is your friend for null values: https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-operators.html#_null_safe – jordivador Jun 21 '18 at 14:19