Search code examples
elasticsearchkibanaelasticsearch-painless

Reindexing elastic-search documents into another index by changing the routing key to a combination of two field values


I have an existing elastic search index with the following document structure, without a routing_key

{
   "_id",
   "feild1"
   "field2"
}

I need to migrate the data into a new index. The structure of the index remains the same with an added routing_key. The routing key needs to be updated to "field1_field2". Is there a simple Kibana script to migrate the data to the new index?


Solution

  • Combination of a simple painless and the reindex API of elastic search could be used to achieve this.

    POST _reindex
    {
      "source": {
        "index": "{old_index_name}",
        "size": {batch_size}
      },
      "dest": {
        "index": "{new_index_name}"
      },
      "script": {
        "lang": "painless",
        "inline": "if (ctx._source.participants.length > 0) {ctx._routing=ctx._source.field1+'-'+ctx._source.field2}"
      }
    }