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

**URL:** <https://discuss.elastic.co/t/null-element-added-to-the-array-if-using-params-in-script-query/83332>\
**Category:** Elasticsearch\
**Created:** [April 23, 2017, 6:29pm UTC](https://discuss.elastic.co/t/null-element-added-to-the-array-if-using-params-in-script-query/83332 "2017-04-23T18:29:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BBerastegui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bberastegui/32/17578_2.png) [@BBerastegui](https://discuss.elastic.co/u/BBerastegui)\
**Post date:** [April 23, 2017, 6:29pm UTC](https://discuss.elastic.co/t/null-element-added-to-the-array-if-using-params-in-script-query/83332/1 "2017-04-23T18:29:13Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 21, 2017, 6:42pm UTC](https://discuss.elastic.co/t/null-element-added-to-the-array-if-using-params-in-script-query/83332/2 "2017-05-21T18:42:09Z")

</div>

Closed on May 21, 2017, 6:42pm UTC

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