'Clone elastic search index with all mappings

I’m trying to clone an elastic search index from staging to prod environment. Couldn’t find a way to do it. All I want to do is to clone the existing index with mappings but not the data.

Can someone point me in the right direction.



Solution 1:[1]

There's no 1-liner for that but this will work if you have the elasticsearch-python module and you only care about the mappings.

from elasticsearch import Elasticsearch

eshost = <YOUR ELASTICSEARCH HOST>
oldindex = <SOURCE INDEX TO COPY>
newindex = <NEW INDEX>

es = Elasticsearch(eshost)

createReply = es.indices.create(index=newindex)
getReplySource = es.indices.get_mapping(index=oldindex)

sourceMappings = getReplySource[oldindex]['mappings']
for doc_type, mapping in sourceMappings.iteritems():
  putReplyTarget = es.indices.put_mapping(doc_type, mapping, newindex)

Solution 2:[2]

In elasticsearch 5.6 and py-elasticsearch 5.5.3, below code works for me:

from elasticsearch import Elasticsearch
es = Elasticsearch("your es url")

old_mapping = es.indices.get_mapping('old_index')

es.indices.create(index='new_index') 
es.indices.put_mapping(index='new_index', doc_type='type name', body=old_mapping['old_index']) 

Solution 3:[3]

Meanwhile, there is the Elasticsearch Clone index API and you can use the Python client to do es.indices.clone(source, target).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Back
Solution 2 DennisLi
Solution 3 Konstantin