# Painless Script to Update Array

**URL:** https://discuss.elastic.co/t/painless-script-to-update-array/233362
**Category:** Elasticsearch
**Tags:** painless
**Created:** [May 19, 2020, 3:43pm UTC](https://discuss.elastic.co/t/painless-script-to-update-array/233362 "2020-05-19T15:43:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mrbarret](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mrbarret](https://discuss.elastic.co/u/mrbarret)
#### Post date: [May 19, 2020, 3:43pm UTC](https://discuss.elastic.co/t/painless-script-to-update-array/233362/1 "2020-05-19T15:43:01Z")

</div>

I am using ELK 7.4.2 and need to write a script to update documents in my cluster. (We have about 20 TB of data so I also ask for advice about optimizations!)

I have documents that look like

```auto
{
   "owner": "Matt",
   "cars": ["Honda", "Kia", "Aston Martin"]
}

```

and I would like to write a script that will remove values from matching documents. For example, remove "Aston Martin" from all the `cars` array for all documents that have "Aston Martin" in the cars array. I have a number of attempts to do this but am having a hard time even debugging the problem(s). Any advice would be appreciated. Here is my latest attempt.

````auto
POST /automobiles/_update_by_query
{
  "query": {
    "term": {
         "cars": "Aston Martin"
    }
  },
  "script_fields": {
    "edit-automobiles-script": {
      "script": {
        "source": """
          def names = ["Aston Martin"];
           for (int j = 0; doc.containsKey("cars") && j < doc["cars"].size(); j++) {
                if (names.contains(doc["cars"][j])) {
                  doc["cars"].values.removeIf(car -> names.contains(car));
                }
            }
""",
        "lang": "painless"
      },
      "ignore_failure": false
    }
  }
}```
````

---

<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: [June 16, 2020, 3:43pm UTC](https://discuss.elastic.co/t/painless-script-to-update-array/233362/2 "2020-06-16T15:43:10Z")

</div>

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