简体   繁体   中英

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 ie (metricbeat-2016.05.30)

Now I want to do following 2 things:

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

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

Thanks in advance !

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM