Error while update using script for array kind of field

I have tags field in document, but while indxing I may not have values for tags field and when tries to add value in it, giving me null pointer error.

mapping for tags

   "type": "text",
    "fields": {
         "keyword": {
          "type": "keyword",
          "ignore_above": 256

update by query using below json

      "script": {
        "inline": "ctx._source.tags.addAll(params.tags); ctx._source.updated_on=params.updated_on; ">             "params": {
          "tags": [
          "updated_on": "2017-06-30T05:06:37.933Z"
        "lang": "painless"
      "query": {
        "match": {
          "stone_id": "8822370208"

Is there any way that I can initialize it as an array
I tried to init it using null_value but not working for me

"tags": {
"type": "keyword",
"ignore_above": 256,
"null_value" : []

Is there any way that I can check it is array before updating using script
if not an array-> init as array then use addAll()

null_value is only applied at indexing time (after update scripts), and only if json null is the value of the field.

In your script, you need to check if the field exists. ctx._source is a Map. So you can do that with ctx._source.containsKey('tags'), and if that returns false, initialize the tags field in source with ctx._source['tags'] = []

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.