diff --git a/Lib/elasticsearch/helpers.py b/Lib/elasticsearch/helpers.py index aec0511..fe2b024 100644 --- a/Lib/elasticsearch/helpers.py +++ b/Lib/elasticsearch/helpers.py @@ -1,6 +1,7 @@ import json import sublime from .utils import show_result_json +from .utils import make_url def scan(client, query=None, scroll='5m', **kwargs): @@ -17,6 +18,11 @@ def scan(client, query=None, scroll='5m', **kwargs): while True: result = client.scroll(scroll_id, params=params) + if 'error' in result.keys(): + import sys + sublime.error_message("Error: {}".format(result['error'])) + sys.exit(1) + if not result['hits']['hits']: break @@ -34,26 +40,29 @@ def reindex(client, source_index, target_index, query=None, target_client=None, target_client = client if target_client is None else target_client + result = { + "_source": make_url(client.base_url, source_index), + "_target": make_url(target_client.base_url, target_index), + } + docs = scan(client, query=query, index=source_index, scroll=scroll, **scan_kwargs) - count = 0 + success, failed = 0, 0 + for doc in docs: - target_client.index(index=target_index, doc_type=doc['_type'], - body=json.dumps(doc['_source']), id=doc['_id']) - count += 1 - sublime.status_message("{0:_>10}".format(count)) + response = target_client.index( + index=target_index, doc_type=doc['_type'], + body=json.dumps(doc['_source']), id=doc['_id']) - result = { - "_source": { - "host": client.base_url, - "index": source_index - }, - "_target": { - "host": target_client.base_url, - "index": target_index - }, - "docs": count - } + if 'error' in response.keys(): + failed += 1 + else: + success += 1 + + sublime.status_message("{0:_>10}".format(success + failed)) + + result['success'] = success + result['failed'] = failed show_result_json(result, sort_keys=True, command=command)