Skip to content

Instantly share code, notes, and snippets.

@simonluijk
Created April 14, 2016 19:14
Show Gist options
  • Save simonluijk/caa39c6fe55eb4e503f59387984448d8 to your computer and use it in GitHub Desktop.
Save simonluijk/caa39c6fe55eb4e503f59387984448d8 to your computer and use it in GitHub Desktop.
Allows to easily migrate Elasticsearch indexes
#!/usr/bin/env python
import sys
from argparse import ArgumentParser
from elasticsearch.helpers import reindex
from elasticsearch import Elasticsearch
class MigrateException(Exception):
pass
def migrate_es_data(source_index, dest_index, query=None, host='localhost'):
es = Elasticsearch(host)
if source_index == dest_index:
msg = 'Can not migrate to index with same name'
raise MigrateException(msg)
if not es.indices.exists(source_index):
msg = 'Source index {0} does not exist'.format(source_index)
raise MigrateException(msg)
if es.indices.exists(dest_index):
msg = 'Destination index {0} already exists'.format(dest_index)
raise MigrateException(msg)
es.indices.create(dest_index)
reindex(es, source_index, dest_index, query=query)
es.indices.refresh(dest_index)
source_count = es.indices.stats(source_index)
source_count = source_count['_all']['primaries']['docs']['count']
dest_count = es.indices.stats(dest_index)
dest_count = dest_count['_all']['primaries']['docs']['count']
return source_count, dest_count
def parse_arguments():
# TODO: Grab query form CLI so we can migrate a subset of the data.
parser = ArgumentParser()
parser.add_argument('--elasticsearch', '-e', type=str, default='localhost')
parser.add_argument('sourceIndex', type=str)
parser.add_argument('destIndex', type=str)
return parser.parse_args()
def main():
args = parse_arguments()
try:
source_count, dest_count = migrate_es_data(args.sourceIndex,
args.destIndex,
host=args.elasticsearch)
print('Migrated {0} of {1} docs form {2} to {3}'.format(
dest_count, source_count, args.sourceIndex, args.destIndex
))
sys.exit(0)
except MigrateException as e:
print(e)
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment