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 must set up the destination index before calling _reindex. Reindex does not copy the settings from the source index. Mappings, shard counts, replicas, and so on must be configured ahead of time.
If mapping of new index is not created before reindex api is executed, mapping will be dynamically created ie data-type of fields will be inferred from first document which is copied. So it is best to create mapping before hand
reindex syntax
POST _reindex
{
"source": {
"index": "<source_index>"
},
"dest": {
"index": "<destination_index>"
},
"script": {
"source":"ctx._source.bene_acc_no += '21'; ctx._source.orig_acc_no += '21';ctx._source.owner_acc_no += '21'"
}
}
If bene_acc_no is of type text
ctx._source.bene_acc_no += '21' will append 21 after the value ex. if bene_acc_no ="20" then new value will be "2021"
If values are integers stored as string and you wish to increment the value then you can use below script "ctx._source.bene_acc_no= (Integer.parseInt(ctx._source.bene_acc_no)+1).toString()"
Above will fail if bene_acc_no has characters