# Painless Script to Concat in ReIndex API

**URL:** <https://discuss.elastic.co/t/painless-script-to-concat-in-reindex-api/143203>\
**Category:** Elasticsearch\
**Created:** [August 6, 2018, 6:25pm UTC](https://discuss.elastic.co/t/painless-script-to-concat-in-reindex-api/143203 "2018-08-06T18:25:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![code\_blue](https://avatars.discourse-cdn.com/v4/letter/c/7ea924/32.png) [@code\_blue](https://discuss.elastic.co/u/code_blue)\
**Post date:** [August 6, 2018, 6:25pm UTC](https://discuss.elastic.co/t/painless-script-to-concat-in-reindex-api/143203/1 "2018-08-06T18:25:36Z")

</div>

I am trying to concat 2 fields using painless script during ReIndex API call. However not able to get the correct syntax.

field3=field1,field2. The below code is throwing a parsing exception

```
    POST _reindex
{
  "source": {
    "index": "sourceIdx",
    "type": "sourxeType"
  },
  "dest": {
    "index": "destinationIdx"
    
  },
  "script": {
    "source": "ctx._source.field3=
    ctx._source.field1 + ',' + ctx._source.field2",
    "lang": "painless"
  }
}
```

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 7, 2018, 8:42pm UTC](https://discuss.elastic.co/t/painless-script-to-concat-in-reindex-api/143203/2 "2018-08-07T20:42:41Z")

</div>

Can you please show what the exception is being thrown? At a glance, it looks like you have a newline in your string script, which is not allowed by json.

---

<div class="post-metadata">

**Author:** ![code\_blue](https://avatars.discourse-cdn.com/v4/letter/c/7ea924/32.png) [@code\_blue](https://discuss.elastic.co/u/code_blue)\
**Post date:** [August 15, 2018, 5:49am UTC](https://discuss.elastic.co/t/painless-script-to-concat-in-reindex-api/143203/3 "2018-08-15T05:49:52Z")

</div>

I got this working. I was not doing a null or empty check for "field1" and "field2" before assigning the value to "field3"

---

<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 12, 2018, 5:49am UTC](https://discuss.elastic.co/t/painless-script-to-concat-in-reindex-api/143203/4 "2018-09-12T05:49:53Z")

</div>

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