Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

I'm following the example over here for updating a set of tags using the partial update in Elasticsearch.

Following is my script:

{

  "script": {

    "lang": "painless",

    "inline": "ctx._source.deviceTags.add(params.tags)",

    "params": {

      "tags": "search"

    }

  }

}

Request URL is:

https://aws-es-service-url/devices/device/123/_update

But I'm getting the following response:

{

    "error": {

        "root_cause": [

            {

                "type": "remote_transport_exception",

                "reason": "[fBaExM8][x.x.x.x:9300][indices:data/write/update[s]]"

            }

        ],

        "type": "illegal_argument_exception",

        "reason": "failed to execute script",

        "caused_by": {

            "type": "script_exception",

            "reason": "runtime error",

            "script_stack": [

                "ctx._source.deviceTags.add(params.tags)",

                "                                 ^---- HERE"

            ],

            "script": "ctx._source.deviceTags.add(params.tags)",

            "lang": "painless",

            "caused_by": {

                "type": "null_pointer_exception",

                "reason": null

            }

        }

    },

    "status": 400

}

Any idea of what I have done wrong?

1 Answer

0 votes
by (44.4k points)

You have two ways to solve this because the deviceTags array is initially null:

1. To make sure that deviceTags will get added to your document initially, use upsert

{

  "script": {

    "lang": "painless",

    "inline": "ctx._source.deviceTags.add(params.tags)",

    "params": {

      "tags": "search"

    }

  },

  "upsert": {

    "deviceTags": ["search"]

  }

}

2. Protect your code against NPE

{

  "script": {

    "lang": "painless",

    "inline": "(ctx._source.deviceTags = ctx._source.deviceTags ?: []).add(params.tags)",

    "params": {

      "tags": "search"

    }

  }

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories

...