ElasticSearch painless script for reindexing

Viewed 6790

We are trying to use following painless script to reindex our data in elasticsearch.

POST _reindex
{
  "source": {
    "index": "metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "inline": "ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'"
  }
}

Reffered from following URL: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html#_reindex_daily_indices

This script works perfect and creates another copy of our all indices. exa: if I have origin index as metricbeat-2016.05.30 after running this script it creates metricbeat-2016.05.30-1 which is exact copy of original index i.e (metricbeat-2016.05.30)

Now I want to do following 2 things:

1] Delete original index i.e metricbeat-2016.05.30
2] Rename reindexed index or copy of original index i.e (metricbeat-2016.05.30-1) back to metricbeat-2016.05.30 i.e original index.

How can we do this ? can we modify above painless script ?

Thanks in advance !

2 Answers

The way I did it was to reindex like in the example from Elasticsearch reference, but instead of appending a "-1" I prepended the index with "temp-":

POST _reindex
{
  "source": {
    "index": "metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'temp-' + ctx._index"
  }
}

This makes it easier to delete the original indices with the pattern "metricbeat-*":

DELETE metricbeat-*

I then reindexed again to get the original name:

POST _reindex
{
  "source": {
    "index": "temp-metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = ctx._index.substring(5)"
  }
}

As a side note, the example in the Elasticsearch reference is unnecessarily complex:

ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'

You get the same result with the code:

ctx._index = ctx._index + '-1'
Related