From 1042022dada72a0ce9be4a0b4c7843a00bce4c17 Mon Sep 17 00:00:00 2001 From: Bryant Khau Date: Tue, 5 May 2020 17:21:36 -0400 Subject: [PATCH 1/2] Avoid negative sizes in Search slice --- elasticsearch_dsl/search.py | 1 + test_elasticsearch_dsl/test_search.py | 1 + 2 files changed, 2 insertions(+) diff --git a/elasticsearch_dsl/search.py b/elasticsearch_dsl/search.py index 5b0196c1d..81e91d22d 100644 --- a/elasticsearch_dsl/search.py +++ b/elasticsearch_dsl/search.py @@ -345,6 +345,7 @@ def __getitem__(self, n): # stop not given. s._extra['from'] = n.start or 0 s._extra['size'] = n.stop - (n.start or 0) if n.stop is not None else 10 + s._extra['size'] = max(s._extra['size'], 0) return s else: # This is an index lookup, equivalent to slicing by [n:n+1]. # If negative index, abort. diff --git a/test_elasticsearch_dsl/test_search.py b/test_elasticsearch_dsl/test_search.py index 592a932d4..bdcf95182 100644 --- a/test_elasticsearch_dsl/test_search.py +++ b/test_elasticsearch_dsl/test_search.py @@ -237,6 +237,7 @@ def test_slice(): assert {'from': 0, 'size': 5} == s[:5].to_dict() assert {'from': 3, 'size': 10} == s[3:].to_dict() assert {'from': 0, 'size': 0} == s[0:0].to_dict() + assert {'from': 20, 'size': 0} == s[20:0].to_dict() def test_index(): s = search.Search() From f7d5b4e362056f45c8050662fcde21e9cfd76bd2 Mon Sep 17 00:00:00 2001 From: bk-equityzen <37187683+bk-equityzen@users.noreply.github.com> Date: Tue, 5 May 2020 17:39:53 -0400 Subject: [PATCH 2/2] Run max on same line when setting size --- elasticsearch_dsl/search.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/elasticsearch_dsl/search.py b/elasticsearch_dsl/search.py index 81e91d22d..59c44c38d 100644 --- a/elasticsearch_dsl/search.py +++ b/elasticsearch_dsl/search.py @@ -344,8 +344,7 @@ def __getitem__(self, n): # Elasticsearch won't get all results so we default to size: 10 if # stop not given. s._extra['from'] = n.start or 0 - s._extra['size'] = n.stop - (n.start or 0) if n.stop is not None else 10 - s._extra['size'] = max(s._extra['size'], 0) + s._extra['size'] = max(0, n.stop - (n.start or 0) if n.stop is not None else 10) return s else: # This is an index lookup, equivalent to slicing by [n:n+1]. # If negative index, abort.