Jyothi
Jyothi

Reputation: 11

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.

Upvotes: 1

Views: 3040

Answers (3)

Konstantin
Konstantin

Reputation: 2629

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

Upvotes: 1

DennisLi
DennisLi

Reputation: 4156

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']) 

Upvotes: 1

Paul Back
Paul Back

Reputation: 1319

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)

Upvotes: 1

Related Questions