0 votes
1 view
in AWS by (18.2k 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 (42.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"

    }

  }

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...