diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-04-12 19:42:31 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-04-12 19:42:31 +0200 |
commit | 07c39548f848ded84bbce8455b974a5e298f1ea2 (patch) | |
tree | 03dfd6936667c3cfb3f2d8b96fd54ed6a004fbd7 /fuzzycat/utils.py | |
parent | 81220a314a6bb179db3554ceb36958417535390f (diff) | |
download | fuzzycat-07c39548f848ded84bbce8455b974a5e298f1ea2.tar.gz fuzzycat-07c39548f848ded84bbce8455b974a5e298f1ea2.zip |
address es hits.total change in ES7
* https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html
Diffstat (limited to 'fuzzycat/utils.py')
-rw-r--r-- | fuzzycat/utils.py | 14 |
1 files changed, 13 insertions, 1 deletions
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) |