From 07c39548f848ded84bbce8455b974a5e298f1ea2 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Mon, 12 Apr 2021 19:42:31 +0200 Subject: address es hits.total change in ES7 * https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html --- fuzzycat/utils.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'fuzzycat/utils.py') diff --git a/fuzzycat/utils.py b/fuzzycat/utils.py index 0f73456..bd7ceed 100644 --- a/fuzzycat/utils.py +++ b/fuzzycat/utils.py @@ -19,6 +19,18 @@ CHEM_FORMULA = re.compile(r"([A-Z]{1,2}[0-9]{1,2})+") ParsedPages = collections.namedtuple("ParsedPages", "start end count") +def es_compat_hits_total(resp): + """ + Given a search response dict, support ES6 and ES7 style total value. See: + https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html + + It is responsibility of the call site to set `track_total_hits` in ES7 to + get an exact number. + """ + try: + return resp["hits"]["total"]["value"] + except TypeError: + return resp["hits"]["total"] def parse_page_string(s): """ @@ -177,7 +189,7 @@ def random_idents_from_query(query="*", if resp.status_code != 200: raise RuntimeError('could not query {} for random item: {}'.format(es, r.url)) payload = resp.json() - if payload["hits"]["total"] < 2: + if es_compat_hits_total(payload) < 2: raise RuntimeError('to few documents') idents = [doc["_source"]["ident"] for doc in payload["hits"]["hits"]] return random.sample(idents, r) -- cgit v1.2.3