# Adding a field by merging existing field while reindexing

**URL:** <https://discuss.elastic.co/t/adding-a-field-by-merging-existing-field-while-reindexing/197994>\
**Category:** Elasticsearch\
**Created:** [September 4, 2019, 7:47am UTC](https://discuss.elastic.co/t/adding-a-field-by-merging-existing-field-while-reindexing/197994 "2019-09-04T07:47:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahul\_raj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahul_raj/32/33566_2.png) [@rahul\_raj](https://discuss.elastic.co/u/rahul_raj)\
**Post date:** [September 4, 2019, 7:47am UTC](https://discuss.elastic.co/t/adding-a-field-by-merging-existing-field-while-reindexing/197994/1 "2019-09-04T07:47:04Z")

</div>

I have an index to which i need to add a field which will merge three existing fields from the same index. Example field1, field2, field3 so unique\_field=field1,field2,field3.  
I am trying below script along with reindexing which gives me error.  
POST \_reindex  
{  
"source":{  
"index" : "old"  
},  
"dest": {  
"index": "new"  
},  
"script": {  
"inline": "ctx.\_source.unique\_field=ctx\_source.field1+ ',' + ctx.\_source.field2+ ',' + ctx.\_source.field3",  
"lang": "painless"  
}  
}

Throws error like -\> "error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "compile error",  
"script\_stack": [  
"... tx.\_source.unique\_field=ctx\_source.field1+ ...",  
" ^---- HERE"  
],  
"script": "ctx.\_source.unique\_field=ctx\_source.field1+ ',' + ctx.\_source.field2+ ',' + ctx.\_source.field3",  
"lang": "painless"  
}  
],  
"type": "script\_exception",  
"reason": "compile error",  
"caused\_by": {  
"type": "illegal\_argument\_exception",  
"reason": "Variable [ctx\_source] is not defined."  
},  
"script\_stack": [  
"... tx.\_source.unique\_field=ctx\_source.application + ...",  
" ^---- HERE"  
],  
"script": "ctx.\_source.unique\_action=ctx\_source.application + ',' + ctx.\_source.subsystem + ',' + ctx.\_source.action",  
"lang": "painless"  
},  
"status": 500

---

<div class="post-metadata">

**Author:** ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)\
**Post date:** [September 6, 2019, 12:18pm UTC](https://discuss.elastic.co/t/adding-a-field-by-merging-existing-field-while-reindexing/197994/2 "2019-09-06T12:18:04Z")

</div>

Hi @rahul_raj,

There is a missing '.' (dot) in the script, `ctx_source.field1` should be `ctx._source.field1`.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [September 6, 2019, 12:19pm UTC](https://discuss.elastic.co/t/adding-a-field-by-merging-existing-field-while-reindexing/197994/3 "2019-09-06T12:19:19Z")

</div>

Looks like there is a missing separator between `ctx` and `_source`. You inline script should be `"ctx._source.unique_field=ctx._source.field1+ ',' + ctx._source.field2+ ',' + ctx._source.field3"` so replacing `ctx_source.field1`by `ctx._source.field1` should fix the problem.

---

<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 4, 2019, 12:19pm UTC](https://discuss.elastic.co/t/adding-a-field-by-merging-existing-field-while-reindexing/197994/4 "2019-10-04T12:19:27Z")

</div>

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