Update All by changing data of a field using a function

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

"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.

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 ?

  "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 ?

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