Null element added to the array if using params in script query

Hi everyone,

I've been trying to solve this issue for a while, and I can't find a reason that explains this behaviour. Probably I'm missing something but I can't find out what.

I'm trying to add an element to an array of a document only if the element is already in that array (I'll try to negate it later, in order to add the element to the array if it's not already there), as follows:

If the array alerts has the value on the param "alerts", add the value itself to the array

{
  "script": "if (ctx._source.alerts.contains(params.alerts)) {ctx._source.alerts.add(params.alerts)}",
  "params": {
    "alerts": "AAA"
  }
}

This is apparently working, but it leaves a null element in the array where the AAA is supposed to be.

This is how the array looks after that query:

{
  "_index": "testindex",
  "_type": "domain",
  "_id": "oneidtest.com",
  "_score": 1,
  "_source": {
    "alerts": [
      "AAA",
      "BBB",
      null
    ],
    "date_modified": "2017-04-23T04:11:41.483328"
  },
  "fields": {
    "date_modified": [
      1492920701483
    ]
  }
}

If I change the parameter to the add function to a fixed value instead of it using the value passed as a parameter, like this:

{
  "script": "if (ctx._source.alerts.contains(params.alerts)) {ctx._source.alerts.add(\"AAA\")}",
  "params": {
    "alerts": "AAA"
  }
}

It works, and it correctly adds the AAA to the array in the document.

As follows:

{
  "_index": "testindex",
  "_type": "domain",
  "_id": "oneidtest.com",
  "_score": 1,
  "_source": {
    "alerts": [
      "AAA",
      "BBB",
      null,
      "AAA"
    ],
    "date_modified": "2017-04-23T04:11:41.483328"
  },
  "fields": {
    "date_modified": [
      1492920701483
    ]
  }
}

Why is it adding a null element to the array when trying to use the params for the script query?

Thanks in advance !

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