# Combine two nested text fields using script

**URL:** <https://discuss.elastic.co/t/combine-two-nested-text-fields-using-script/245718>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 20, 2020, 7:21am UTC](https://discuss.elastic.co/t/combine-two-nested-text-fields-using-script/245718 "2020-08-20T07:21:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AntonyUkken](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@AntonyUkken](https://discuss.elastic.co/u/AntonyUkken)\
**Post date:** [August 20, 2020, 7:21am UTC](https://discuss.elastic.co/t/combine-two-nested-text-fields-using-script/245718/1 "2020-08-20T07:21:40Z")

</div>

I need to combine two text nested fields to a new field using scripts.

```
POST script/_update_by_query
{
  "script": {
    "source": """
      ctx._source.example = ctx._source.field1 + '_' + ctx._source.field2
    """,
    "lang": "painless"
  }
}

```

I used the above script to combine two fields, but the same is not able to do for nested fields  
{  
"error" : {  
"root\_cause" : [  
{  
"type" : "script\_exception",  
"reason" : "runtime error",  
"script\_stack" : [  
"ctx.\_source.last1 = ctx._source.log.match.id + '_' + ctx.\_source.resourceId\n ",  
" ^---- HERE"  
],  
"script" : "\n ctx.\_source.last1 = ctx._source.log.match.id + '_' + ctx.\_source.resourceId\n ",  
"lang" : "painless",  
"position" : {  
"offset" : 48,  
"start" : 7,  
"end" : 87  
}  
}  
],  
"type" : "script\_exception",  
"reason" : "runtime error",  
"script\_stack" : [  
"ctx.\_source.last1 = ctx._source.log.match.id + '_' + ctx.\_source.resourceId\n ",  
" ^---- HERE"  
],  
"script" : "\n ctx.\_source.last1 = ctx._source.log.match.id + '_' + ctx.\_source.resourceId\n ",  
"lang" : "painless",  
"position" : {  
"offset" : 48,  
"start" : 7,  
"end" : 87  
},  
"caused\_by" : {  
"type" : "illegal\_argument\_exception",  
"reason" : "Illegal list shortcut value [id]."  
}  
},  
"status" : 400  
}

Getting this error.

```
{
  "_index": "script",
  "_type": "_doc",
  "_id": "bbda6005174a13e4b457ebf9ce064cf0",
  "_score": 1,
  "_source": {
    "app": "ADC",
    "field2": "KU1T7GAGIS",
    "example": "XGHCWMNXA4_KU1T7GAGIS",
    "log": {
      "match": [
        {
          "zone": "val",
          "id": "999001020"
        }
      ]
    },
    "field1": "XGHCWMNXA4"
  }
}

```

This is my document

---

<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:** [September 17, 2020, 7:21am UTC](https://discuss.elastic.co/t/combine-two-nested-text-fields-using-script/245718/2 "2020-09-17T07:21:42Z")

</div>

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