From 31a21a575f73ea23779e5da075d68522ac930bdc Mon Sep 17 00:00:00 2001 From: Md Nazrul Islam Date: Tue, 20 Aug 2019 11:45:54 +0600 Subject: [PATCH 1/2] Issue #63 Now ensuring we unicode string value would work for both python2 and python3 [nazrulworld] --- collective/elasticsearch/hook.py | 17 +++++++++++++---- docs/history.rst | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/collective/elasticsearch/hook.py b/collective/elasticsearch/hook.py index 280aabd..82ea2f8 100644 --- a/collective/elasticsearch/hook.py +++ b/collective/elasticsearch/hook.py @@ -16,6 +16,7 @@ import logging import random +import six import time import traceback import transaction @@ -161,8 +162,12 @@ def get_index_data(obj, es): # Ignore errors in converting to unicode, so json.dumps # does not barf when we're trying to send data to ES. - if isinstance(value, str): - value = unicode(value, 'utf-8', 'ignore') + if six.PY2: + if isinstance(value, str): + value = six.text_type(value, 'utf-8', 'ignore') + else: + if isinstance(value, bytes): + value = value.decode('utf-8', 'ignore') index_data[index_name] = value @@ -175,8 +180,12 @@ def get_index_data(obj, es): if indexer is not None: try: val = indexer() - if isinstance(value, str): - val = unicode(val, 'utf-8', 'ignore') + if six.PY2: + if isinstance(value, str): + value = six.text_type(value, 'utf-8', 'ignore') + else: + if isinstance(value, bytes): + value = value.decode('utf-8', 'ignore') index_data[name] = val except Exception: logger.error('Error indexing value: %s: %s\n%s' % ( diff --git a/docs/history.rst b/docs/history.rst index 814b940..3ede74b 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -4,6 +4,8 @@ Changelog 3.0.4 (unreleased) ------------------ +- `Issue#63 `_ Now ensuring unicode value would for both python2 and python3 case. [nazrulworld] + - Now possible to search by other than `Title`, `Description` and `SearchableText` indexes. [nazrulworld] From 0532c706937d87227b42bb9c0e651e454742dc45 Mon Sep 17 00:00:00 2001 From: Md Nazrul Islam Date: Tue, 20 Aug 2019 13:40:48 +0600 Subject: [PATCH 2/2] Py2TOPy3 compatibility: we force integer type result key as in python3 lower number division by uppper number (1/50) returns fractional value instead of 0 (like python2) --- collective/elasticsearch/es.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collective/elasticsearch/es.py b/collective/elasticsearch/es.py index 20aaa0b..ac03bd9 100644 --- a/collective/elasticsearch/es.py +++ b/collective/elasticsearch/es.py @@ -82,7 +82,7 @@ def __getitem__(self, key): raise IndexError if key >= 0: - result_key = (key / self.bulk_size) * self.bulk_size + result_key = int(key / self.bulk_size) * self.bulk_size start = result_key result_index = key % self.bulk_size elif key < 0: