Skip to content

Commit 6d27e06

Browse files
simplify slicing logic
1 parent ba25ef4 commit 6d27e06

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

elasticsearch_dsl/search_base.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,22 @@ def __getitem__(self, n):
363363
# Elasticsearch won't get all results so we default to size: 10 if
364364
# stop not given.
365365
old_from = s._extra.get("from", 0)
366-
old_to = old_from + s._extra.get(
367-
"size", slice_stop or (slice_start or old_from) + 10
368-
)
366+
if "size" in s._extra:
367+
old_to = old_from + s._extra["size"]
368+
elif slice_stop is not None:
369+
# inherit a size from the given slice
370+
old_to = old_from + slice_stop
371+
elif slice_start is not None:
372+
# assume a default size of 10 from the given slice start
373+
old_to = old_from + slice_start + 10
374+
else:
375+
# with no other information, the default size is 10
376+
old_to = old_from + 10
369377
new_from = old_from + (slice_start or 0)
370-
new_to = (
371-
min(old_to, old_from + slice_stop) if slice_stop is not None else old_to
372-
)
378+
if slice_stop is not None:
379+
new_to = min(old_to, old_from + slice_stop)
380+
else:
381+
new_to = old_to
373382
s._extra["from"] = new_from
374383
s._extra["size"] = max(0, new_to - new_from)
375384
return s

tests/_async/test_search.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ def test_slice():
365365
assert {"from": 10, "size": 0} == s[:5][10:].to_dict()
366366
assert {"from": 12, "size": 0} == s[:5][10:][2:].to_dict()
367367
assert {"from": 15, "size": 0} == s[10:][:5][5:].to_dict()
368+
assert {"from": 0, "size": 10} == s[:].to_dict()
369+
with raises(ValueError):
370+
s[-1:]
371+
with raises(ValueError):
372+
s[4:-1]
373+
with raises(ValueError):
374+
s[-3:-2]
368375

369376

370377
def test_index():
@@ -373,6 +380,8 @@ def test_index():
373380
assert {"from": 3, "size": 1} == s[3][0].to_dict()
374381
assert {"from": 8, "size": 0} == s[3][5].to_dict()
375382
assert {"from": 4, "size": 1} == s[3:10][1].to_dict()
383+
with raises(ValueError):
384+
s[-3]
376385

377386

378387
def test_search_to_dict():

tests/_sync/test_search.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ def test_slice():
365365
assert {"from": 10, "size": 0} == s[:5][10:].to_dict()
366366
assert {"from": 12, "size": 0} == s[:5][10:][2:].to_dict()
367367
assert {"from": 15, "size": 0} == s[10:][:5][5:].to_dict()
368+
with raises(ValueError):
369+
s[-1:]
370+
with raises(ValueError):
371+
s[4:-1]
372+
with raises(ValueError):
373+
s[-3:-2]
368374

369375

370376
def test_index():
@@ -373,6 +379,8 @@ def test_index():
373379
assert {"from": 3, "size": 1} == s[3][0].to_dict()
374380
assert {"from": 8, "size": 0} == s[3][5].to_dict()
375381
assert {"from": 4, "size": 1} == s[3:10][1].to_dict()
382+
with raises(ValueError):
383+
s[-3]
376384

377385

378386
def test_search_to_dict():

0 commit comments

Comments
 (0)