Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch painless script for reindexing

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 !

like image 270
SSG Avatar asked Oct 21 '25 11:10

SSG


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'
like image 83
Bjorn Avatar answered Oct 24 '25 06:10

Bjorn


you cannot rename an index. You could use aliases however, after you deleted the original index.

like image 37
alr Avatar answered Oct 24 '25 05:10

alr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!