# Update All by changing data of a field using a function

**URL:** <https://discuss.elastic.co/t/update-all-by-changing-data-of-a-field-using-a-function/315476>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [September 29, 2022, 4:05pm UTC](https://discuss.elastic.co/t/update-all-by-changing-data-of-a-field-using-a-function/315476 "2022-09-29T16:05:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadiki-dev](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sadiki-dev](https://discuss.elastic.co/u/sadiki-dev)\
**Post date:** [September 29, 2022, 4:05pm UTC](https://discuss.elastic.co/t/update-all-by-changing-data-of-a-field-using-a-function/315476/1 "2022-09-29T16:05:49Z")

</div>

I have some data in an index for example called _ **products** _.  
the data stored has structure like this :

```json
[
{
"name" : " product A ",
"price": 1000,
"quantity": 10
},
{
"name" : " product B ",
"price": 2000,
"quantity": 20
}
]

```

I noticed that I have some not trimmed fields, I want to trim them with another typescript function.

```nohighlight
const trimField = (field: string) => field.trim()

```

I will to add newField with the trimed field like that using update by query :  
How I can do it by calling the trim function I already have ?

```auto
client.updateByQuery(
{
  "script": {
    "source": "ctx._source.newName= params.name",
    "params": {
      "name": "ayoub"
    }
  },
    "query": {
      "match_all": {}
    }
})

```

or I need to get the old name and update one by one.

I search for a way to updateAll in the same call.

How can I do it ?

---

<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:** [October 27, 2022, 4:06pm UTC](https://discuss.elastic.co/t/update-all-by-changing-data-of-a-field-using-a-function/315476/2 "2022-10-27T16:06:26Z")

</div>

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