Skip to content

Commit

Permalink
Update Reindex helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunihiko Kido committed May 7, 2015
1 parent 79d4675 commit e59a0d4
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Lib/elasticsearch/helpers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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

Expand All @@ -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)

0 comments on commit e59a0d4

Please sign in to comment.