Hey, today I happened to write a script to solve a specific problem that it looks like a good deal of people face: renaming a given Elasticsearch index. Naturally, there are documented solutions but I didnāt find quickly a script that would get me where I wantedāāāall the data from an index named now being queryable in an index named with all the properties set. a b Note.: the following code is aimed at Elasticsearch 2.4.6. Here it comes then. Reindexing step byĀ step There are four steps to get towards our goal: Create an Elasticsearch index and populate it with some data; Get the configurations of the original index; Create the new index with the desired configuration; Run action; _reindex Drop the old index. 0. Create an Elasticsearch index and populate it with someĀ data To create an index using the default parameters (e.g, number of shards and replicas) we can issue a against the Elasticsearch HTTP endpoint specifying the desired index (in this case, : POST acme-production Which, naturally, has no data indexed: Now we populate it with some data: Which we can verify by looking again at the endpoint: /_cat/indices 1. Get the configurations of the originalĀ index Because the renaming is nothing more than ācreate, copy and deleteā we need to create a new index with the properties from the old one. To properly achieve that we must then copy the old configuration: ps.: here Iām making use of , the lightweight command-line JSON processor, in order to get the mappings and settings objects from within the bigger object returned by the call to . This way weāre able to assign that to a variable and then make use of it later. jq /<index>/_settings,_mappings 2. Create the new index with the desired configuration Using the old configuration (stored in the variable) weāre able to create the the index based on it: index_config ps.: even though thereās an _uuid_ in the _$index_config_ object there, it doesnāt matter - itāll get replaced by a new _uuid_ in the new index. 3. Run action _reindex Having both indices properly configured weāre ready to have the data from the old index in the new one: By this time you should already have your new index populated. Now itās a matter of deleting the old index: 4. Drop the oldĀ index If you have no intention of making use of the old index, now itās time to drop it: What aboutĀ aliases? I received some pretty interesting feedback on Reddit that Iād like to share here. It turns out that sometimes we can avoid ing by making use of aliases (see ). reindex Elasticsearch Indices Aliases The idea is that when we need to reference whatās covered by an index by using another name we can create some kind of āpointerā to the real index and perform all the normal operations against this pointer (alias). The API that allows doing that allows us to essentially (create, remove, update and delete) aliases, making it totally possible for us to perform what we want: achieve a ārenamingā of an index, even if only virtually. CRUD Letās do it then. First, create an index just like before and add some data: acme-production then create an named : alias acme-staging if we check the indices weāll see that we donāt have any new index though: But that we do have aliases: which allows us to perform queries against and retrieve data from : acme-staging acme-production Now, what if we want to disallow requests to the old index? As if we had really renamed it and not duplicated? Then we need to close the old index using the : open/close index api curl -XPOST http://localhost:9200/acme-production/_close then we can try to get from : acme-production Cool, what we wanted, huh? Now, if we try to get from : acme-staging we canāt retrieve either. It sounds logical to me that we canāt as the alias is just a pointer to the other index (which was closed). So, to sum up, if you want to have new indices to point to an existing one (as if you were renaming), aliases will save you and youāll need to perform 0 copying of data. If you need to have something like ārenameā and disallow access to the old index, then wonāt help you (will have to use the strategy. alias reindex + delete I never used aliases before and itās pretty good to know that they exist! It can definitely be very useful some times. Closing thoughts and resources As someone who never really dug deep into how Elasticsearch works, I found very easy the whole concept of reindexing. The official documentation is pretty good and with it, I was able to quickly solve the problem. Kudos Elasticsearch team! Thanks, finis Originally published at ops.tips on November 21, 2017.