Orkun
Orkun

Reputation: 522

How to modify multiple index names in Elasticsearch

I have indices like below,

europe_serbia_people
europe_germany_people

etc. (I have a lot of indices)

I want to change all indices with putting "old" keyword to end of indices.

I mean the indices that starts with europe should ends with "old"

I have checked _reindex api but I don't want to merge my indices , and i can't call this api one by one for all indices cause i have a lot of it.

The expected results should be;

europe_serbia_people_old
europe_germany_people_old

How can i achieve the expected results ?

Thanks for answering

Upvotes: 0

Views: 93

Answers (1)

Val
Val

Reputation: 217564

It is definitely possible to use the _reindex API for this, by using a script that will modify the target index name, like this:

POST _reindex
{
  "source": {
    "index": "europe_*"
  },
  "dest": {
    "index": "dummy"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = ctx._index + '_old'"
  }
}

Another way to do it more efficiently is to proceed via snapshots if you have any (or you can create one beforehand). When restoring snapshots, you can rename the indices you restore, as follows:

POST /_snapshot/my_backup/snapshot_1/_restore
{
  "indices": "europe_*",
  "ignore_unavailable": true,
  "include_global_state": false,              
  "include_aliases": false,
  "rename_pattern": "(.+)",            <--- capture the full name
  "rename_replacement": "$1_old"       <--- append _old to the original name
}

Upvotes: 2

Related Questions