# How update the nested element in ES through Groovy

**URL:** https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184
**Category:** Elasticsearch
**Created:** [September 2, 2017, 5:42am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184 "2017-09-02T05:42:40Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![yogeswararao](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yogeswararao](https://discuss.elastic.co/u/yogeswararao)
#### Post date: [September 2, 2017, 5:42am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/1 "2017-09-02T05:42:41Z")

</div>

How to update the nested field values in the ES Groovey.

curl -XPOST 'localhost:9200/iapi-log-dev-lorain/log/Sample.Test/\_update' -d '{"script" : "ctx.\_source.@fields.Metrics.TOTAL\_MSGS = TOTAL\_MSGS","params" : {"TOTAL\_MSGS" : 2200 } }'

{"error":"RemoteTransportException[[iapi-dev-lorain-node-2][inet[/11.120.39.149:9301]][indices:data/write/update]]; nested: ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptExecutionException[MissingFieldException[No such field: fields for class: java.util.LinkedHashMap]]; ","status":400}

Here is the ES document:

{  
"@type": "iapi.log",  
"@timestamp": "2017-09-02T03:34:45.399Z",  
"@source\_host": "my local desktop",  
"@message": "TEST",  
"@environment": "DEV",  
"@fields": {  
"AppID": "ABC",  
"TransactionID": "12-203-809-49181-42-131-822980900282-5242-146873621",  
"CorrelationID": "6f39b6d3-85fd-44e1-b30a-bb1dced34f25",  
"TransactionDomain": "Inventory",  
"ElapsedTime": "0",  
"AppDomain": "DEV",  
"Metrics": {  
"Sample.Test": "DCR\_EMS\_STATS\_RcvToCollSales",  
"TOTAL\_MSGS": 2000,  
"MSGS\_PENDING": 12,  
"MSGS\_FAILED": 1100  
}  
}  
}'

---

<div class="post-metadata">

### Author: ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)
#### Post date: [September 5, 2017, 1:50am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/2 "2017-09-05T01:50:01Z")

</div>

> [@yogeswararao](#):
>
> ctx.\_source.@fields.Metrics.TOTAL\_MSGS = TOTAL\_MSGS

This syntax is not correct. Try with:

```auto
ctx._source['@fields'].Metrics.TOTAL_MSG = TOTAL_MSGS

```

---

<div class="post-metadata">

### Author: ![yogeswararao](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yogeswararao](https://discuss.elastic.co/u/yogeswararao)
#### Post date: [September 5, 2017, 2:24am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/3 "2017-09-05T02:24:01Z")

</div>

Thiago Thanks for the reponse.

still i am seeing the error saying that unable to see reslove class field.

"script": "ctx.\_source.Metrics.TOTAL\_MSGS =+ my\_modifier",  
"params": {  
"my\_modifier": 8

{"error":"RemoteTransportException[[iapi-dev-lorain-node-2][inet[/11.120.39.149:9301]][indices:data/write/update]]; nested: ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptCompilationException[MultipleCompilationErrorsException[startup failed:\nb247a3641a4be9c42402976784c2b70a21f0612b: 1: unable to resolve class fields , unable to find class for annotation\n @ line 1, column 13.\n ctx.\_source[@fields].Metrics.TOTAL\_MSGS =+ my\_modifier\n ^\n\n1 error\n]]; ","status":400}[apache@esu1l338 scripts]$

---

<div class="post-metadata">

### Author: ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)
#### Post date: [September 5, 2017, 2:28am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/4 "2017-09-05T02:28:21Z")

</div>

According to the exception, you have removed the single-quote `'`. That's not what I suggested. Here is what I suggested:

```auto
ctx._source['@fields'].Metrics.TOTAL_MSGS = TOTAL_MSGS

```

Notice that `'@fields'` is surrounded by single-quotes.

---

<div class="post-metadata">

### Author: ![yogeswararao](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yogeswararao](https://discuss.elastic.co/u/yogeswararao)
#### Post date: [September 5, 2017, 2:55am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/5 "2017-09-05T02:55:49Z")

</div>

Thiago : Please find the complete script.

**Request :**

curl -XPOST 'localhost:9200/iapi-log-dcr-dev-lorain/log/Sample.Test/\_update' -d '{  
"functions": [  
{  
"script\_score": {  
"script": "ctx.\_source['@fields'].Metrics.TOTAL\_MSGS =+ my\_modifier",  
"params": {  
"my\_modifier": 8  
}  
}  
}  
]  
}'

**Exception:**

{"error":"RemoteTransportException[[iapi-dev-lorain-node-2][inet[/11.120.39.149:9301]][indices:data/write/update]]; nested: ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptCompilationException[MultipleCompilationErrorsException[startup failed:\nb247a3641a4be9c42402976784c2b70a21f0612b: 1: unable to resolve class fields , unable to find class for annotation\n @ line 1, column 13.\n ctx.\_source[@fields].Metrics.TOTAL\_MSGS =+ my\_modifier\n ^\n\n1 error\n]]; ","status":400}[apache@esu1l338 scripts]$

---

<div class="post-metadata">

### Author: ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)
#### Post date: [September 5, 2017, 2:57am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/6 "2017-09-05T02:57:13Z")

</div>

What is the Elasticsearch version?

---

<div class="post-metadata">

### Author: ![yogeswararao](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yogeswararao](https://discuss.elastic.co/u/yogeswararao)
#### Post date: [September 5, 2017, 3:01am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/7 "2017-09-05T03:01:25Z")

</div>

ES 1.7.5

---

<div class="post-metadata">

### Author: ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)
#### Post date: [September 5, 2017, 3:20am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/8 "2017-09-05T03:20:35Z")

</div>

This is a shell syntax/quoting issue. Using double-quotes will also works. Try with the following:

```bash
$ curl -XPOST 'localhost:9200/iapi-log-dcr-dev-lorain/log/Sample.Test/_update' -d '{
  "functions": [
    {
      "script_score": {
        "script": "ctx._source[\"@fields\"].Metrics.TOTAL_MSGS =+ my_modifier",
        "params": {
          "my_modifier": 8
        }
      }
    }
  ]
}'

```

---

<div class="post-metadata">

### Author: ![yogeswararao](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yogeswararao](https://discuss.elastic.co/u/yogeswararao)
#### Post date: [September 5, 2017, 3:29am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/9 "2017-09-05T03:29:19Z")

</div>

It is working fine Thiago.

Thanks a lot.

May I know what is issue for below statement ?

ctx.\_source. @fields.Metrics.TOTAL\_MSGS

---

<div class="post-metadata">

### Author: ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)
#### Post date: [September 5, 2017, 3:31am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/10 "2017-09-05T03:31:56Z")

</div>

The `@` is not a valid field name character that can be used with that field access syntax in particular.

---

<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 3, 2017, 3:31am UTC](https://discuss.elastic.co/t/how-update-the-nested-element-in-es-through-groovy/99184/11 "2017-10-03T03:31:56Z")

</div>

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