How to append a character to a string in elasticsearch?

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?

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.

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.

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' " ?

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

1 Like

Thank you for your clarification

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