Skip to content

Commit

Permalink
Request and store es version info and use to modify the object select…
Browse files Browse the repository at this point in the history
…or for the number of hits. Issue taraslayshchuk#74
  • Loading branch information
dkoneill committed Nov 7, 2019
1 parent b119cd9 commit 0a5de14
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion es2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import progressbar
from backports import csv
from functools import wraps
from packaging import version


FLUSH_BUFFER = 1000 # Chunk of docs to flush in temp file
Expand Down Expand Up @@ -61,6 +62,7 @@ def create_connection(self):
client_cert=self.opts.client_cert, client_key=self.opts.client_key)
es.cluster.health()
self.es_conn = es
self.es_info = es.info()

@retry(elasticsearch.exceptions.ConnectionError, tries=TIMES_TO_TRY)
def check_indexes(self):
Expand Down Expand Up @@ -124,7 +126,14 @@ def next_scroll(scroll_id):
print('Sorting by: {}.'.format(', '.join(self.opts.sort)))

res = self.es_conn.search(**search_args)
self.num_results = res['hits']['total']

# Breaking change in ES 7.0 for total hits
# https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#hits-total-now-object-search-response

if version.parse(self.es_info['version']['number']) < version.parse("7.0.0"):
self.num_results = res['hits']['total']
else:
self.num_results = res['hits']['total']['value']

print('Found {} results.'.format(self.num_results))
if self.opts.debug_mode:
Expand Down

0 comments on commit 0a5de14

Please sign in to comment.