# How to append a character to a string in elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778>\
**Category:** Elasticsearch\
**Created:** [March 16, 2020, 2:24pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778 "2020-03-16T14:24:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![guibvieira](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@guibvieira](https://discuss.elastic.co/u/guibvieira)\
**Post date:** [March 16, 2020, 2:24pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778/1 "2020-03-16T14:24:06Z")

</div>

I am attempting to write a transformation while using the \_reindex API. The idea is that I'm taking an existing index, I want to reindex it and append a number to the end of a few hash string parameters in the process (type string). Is this a viable solution to do that?

```auto
POST <index>/<index_type>/_reindex
{
  "source": {
    "index": "<index_name>"
  },
  "dest": {
    "index": "<new_index_name>",
  },
  "script": {
        "source": "ctx._source.bene_acc_no += '21' ",
        "source" : "ctx._source.orig_acc_no += '21' ",
        "source" : "ctx._source.owner_acc_no += '21' "
  }
}

```

Or can I simply use `"source" : "ctx._source.owner_acc_no++"` ?

Finally, do I have to worry about mapping with reindexing, or can I assume this will be done automatically when reindexing.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 16, 2020, 3:50pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778/2 "2020-03-16T15:50:53Z")

</div>

You do have to create the index with the correct mapping yourself.

If you want to increment an integer, you can use the `++` notation, however your example is appended a string to a value, which is something completely different. Both work however.

---

<div class="post-metadata">

**Author:** ![guibvieira](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@guibvieira](https://discuss.elastic.co/u/guibvieira)\
**Post date:** [March 16, 2020, 4:04pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778/3 "2020-03-16T16:04:20Z")

</div>

Thank you for your quick response.

Yes, in this scenario I want to append a string to a string field, e.g. bene\_acc\_no, orig\_acc\_no. In this scenario will the "source" : "ctx.\_source.owner\_acc\_no++" notation work or do I have to use the appending notation "ctx.\_source.owner\_acc\_no += '21' " ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 16, 2020, 4:10pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778/4 "2020-03-16T16:10:08Z")

</div>

that depends on the type of the field. If you do not want numeric addition, always go with the string based append.

---

<div class="post-metadata">

**Author:** ![guibvieira](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@guibvieira](https://discuss.elastic.co/u/guibvieira)\
**Post date:** [March 16, 2020, 4:14pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778/5 "2020-03-16T16:14:28Z")

</div>

Thank you for your clarification

---

<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:** [April 13, 2020, 4:27pm UTC](https://discuss.elastic.co/t/how-to-append-a-character-to-a-string-in-elasticsearch/223778/6 "2020-04-13T16:27:15Z")

</div>

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