Skip to content

Commit e765c4c

Browse files
simplified slicing logic even more
1 parent 0b310dd commit e765c4c

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

elasticsearch_dsl/search_base.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,25 +360,25 @@ def __getitem__(self, n):
360360
raise ValueError("Search does not support negative indexing.")
361361
slice_start = n
362362
slice_stop = n + 1
363-
old_from = s._extra.get("from", 0)
364-
if "size" in s._extra:
365-
old_to = old_from + s._extra["size"]
366-
elif slice_stop is not None:
367-
# inherit a size from the given slice
368-
old_to = old_from + slice_stop
369-
elif slice_start is not None:
370-
# assume a default size of 10 from the given slice start
371-
old_to = old_from + slice_start + 10
363+
364+
old_from = s._extra.get("from")
365+
old_to = (old_from or 0) + s._extra["size"] if "size" in s._extra else None
366+
367+
if slice_start is not None:
368+
new_from = (old_from or 0) + slice_start
372369
else:
373-
# with no other information, the default size is 10
374-
old_to = old_from + 10
375-
new_from = old_from + (slice_start or 0)
370+
new_from = old_from
376371
if slice_stop is not None:
377-
new_to = min(old_to, old_from + slice_stop)
372+
new_to = (old_from or 0) + slice_stop
373+
if old_to is not None and old_to < new_to:
374+
new_to = old_to
378375
else:
379376
new_to = old_to
380-
s._extra["from"] = new_from
381-
s._extra["size"] = max(0, new_to - new_from)
377+
378+
if new_from is not None:
379+
s._extra["from"] = new_from
380+
if new_to is not None:
381+
s._extra["size"] = max(0, new_to - (new_from or 0))
382382
return s
383383

384384
@classmethod

tests/_async/test_search.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,18 @@ def test_collapse():
357357
def test_slice():
358358
s = AsyncSearch()
359359
assert {"from": 3, "size": 7} == s[3:10].to_dict()
360-
assert {"from": 0, "size": 5} == s[:5].to_dict()
361-
assert {"from": 3, "size": 10} == s[3:].to_dict()
360+
assert {"size": 5} == s[:5].to_dict()
361+
assert {"from": 3} == s[3:].to_dict()
362362
assert {"from": 0, "size": 0} == s[0:0].to_dict()
363363
assert {"from": 20, "size": 0} == s[20:0].to_dict()
364364
assert {"from": 10, "size": 5} == s[10:][:5].to_dict()
365365
assert {"from": 10, "size": 0} == s[:5][10:].to_dict()
366-
assert {"from": 0, "size": 10} == s[:10][:40].to_dict()
367-
assert {"from": 0, "size": 10} == s[:40][:10].to_dict()
368-
assert {"from": 0, "size": 40} == s[:40][:80].to_dict()
366+
assert {"size": 10} == s[:10][:40].to_dict()
367+
assert {"size": 10} == s[:40][:10].to_dict()
368+
assert {"size": 40} == s[:40][:80].to_dict()
369369
assert {"from": 12, "size": 0} == s[:5][10:][2:].to_dict()
370370
assert {"from": 15, "size": 0} == s[10:][:5][5:].to_dict()
371-
assert {"from": 0, "size": 10} == s[:].to_dict()
371+
assert {} == s[:].to_dict()
372372
with raises(ValueError):
373373
s[-1:]
374374
with raises(ValueError):

tests/_sync/test_search.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,18 @@ def test_collapse():
357357
def test_slice():
358358
s = Search()
359359
assert {"from": 3, "size": 7} == s[3:10].to_dict()
360-
assert {"from": 0, "size": 5} == s[:5].to_dict()
361-
assert {"from": 3, "size": 10} == s[3:].to_dict()
360+
assert {"size": 5} == s[:5].to_dict()
361+
assert {"from": 3} == s[3:].to_dict()
362362
assert {"from": 0, "size": 0} == s[0:0].to_dict()
363363
assert {"from": 20, "size": 0} == s[20:0].to_dict()
364364
assert {"from": 10, "size": 5} == s[10:][:5].to_dict()
365365
assert {"from": 10, "size": 0} == s[:5][10:].to_dict()
366-
assert {"from": 0, "size": 10} == s[:10][:40].to_dict()
367-
assert {"from": 0, "size": 10} == s[:40][:10].to_dict()
368-
assert {"from": 0, "size": 40} == s[:40][:80].to_dict()
366+
assert {"size": 10} == s[:10][:40].to_dict()
367+
assert {"size": 10} == s[:40][:10].to_dict()
368+
assert {"size": 40} == s[:40][:80].to_dict()
369369
assert {"from": 12, "size": 0} == s[:5][10:][2:].to_dict()
370370
assert {"from": 15, "size": 0} == s[10:][:5][5:].to_dict()
371-
assert {"from": 0, "size": 10} == s[:].to_dict()
371+
assert {} == s[:].to_dict()
372372
with raises(ValueError):
373373
s[-1:]
374374
with raises(ValueError):

0 commit comments

Comments
 (0)