From 36967e5307023f7eef97a018eaab5e80650e0991 Mon Sep 17 00:00:00 2001 From: Yuri Khrustalev Date: Wed, 22 Oct 2014 14:39:11 +0400 Subject: [PATCH] make scan helper deal with empty response, refs #142 --- elasticsearch/helpers/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/elasticsearch/helpers/__init__.py b/elasticsearch/helpers/__init__.py index 2a706070..a4d7f080 100644 --- a/elasticsearch/helpers/__init__.py +++ b/elasticsearch/helpers/__init__.py @@ -173,7 +173,9 @@ def scan(client, query=None, scroll='5m', **kwargs): # initial search to resp = client.search(body=query, search_type='scan', scroll=scroll, **kwargs) - scroll_id = resp['_scroll_id'] + scroll_id = resp.get('_scroll_id') + if scroll_id is None: + return while True: resp = client.scroll(scroll_id, scroll=scroll) @@ -181,7 +183,9 @@ def scan(client, query=None, scroll='5m', **kwargs): break for hit in resp['hits']['hits']: yield hit - scroll_id = resp['_scroll_id'] + scroll_id = resp.get('_scroll_id') + if scroll_id is None: + break def reindex(client, source_index, target_index, target_client=None, chunk_size=500, scroll='5m'): """