Skip to content

Commit

Permalink
Added support for slicing multiple times in Search class
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Apr 9, 2024
1 parent cdb14b6 commit f8d0e9f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
23 changes: 13 additions & 10 deletions elasticsearch_dsl/search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,23 @@ def __getitem__(self, n):
# If negative slicing, abort.
if n.start and n.start < 0 or n.stop and n.stop < 0:
raise ValueError("Search does not support negative slicing.")
# 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"] = max(
0, n.stop - (n.start or 0) if n.stop is not None else 10
)
return s
start = n.start
stop = n.stop
else: # This is an index lookup, equivalent to slicing by [n:n+1].
# If negative index, abort.
if n < 0:
raise ValueError("Search does not support negative indexing.")
s._extra["from"] = n
s._extra["size"] = 1
return s
start = n
stop = n + 1
# Elasticsearch won't get all results so we default to size: 10 if
# stop not given.
old_from = s._extra.get("from", 0)
old_to = old_from + s._extra.get("size", stop or (start or old_from) + 10)
new_from = old_from + (start or 0)
new_to = min(old_to, old_from + stop) if stop is not None else old_to
s._extra["from"] = new_from
s._extra["size"] = max(0, new_to - new_from)
return s

@classmethod
def from_dict(cls, d):
Expand Down
6 changes: 6 additions & 0 deletions tests/_async/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,17 @@ def test_slice():
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()
assert {"from": 10, "size": 5} == s[10:][:5].to_dict()
assert {"from": 10, "size": 0} == s[:5][10:].to_dict()
assert {"from": 12, "size": 0} == s[:5][10:][2:].to_dict()
assert {"from": 15, "size": 0} == s[10:][:5][5:].to_dict()


def test_index():
s = AsyncSearch()
assert {"from": 3, "size": 1} == s[3].to_dict()
assert {"from": 3, "size": 1} == s[3][0].to_dict()
assert {"from": 8, "size": 0} == s[3][5].to_dict()


def test_search_to_dict():
Expand Down
6 changes: 6 additions & 0 deletions tests/_sync/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,17 @@ def test_slice():
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()
assert {"from": 10, "size": 5} == s[10:][:5].to_dict()
assert {"from": 10, "size": 0} == s[:5][10:].to_dict()
assert {"from": 12, "size": 0} == s[:5][10:][2:].to_dict()
assert {"from": 15, "size": 0} == s[10:][:5][5:].to_dict()


def test_index():
s = Search()
assert {"from": 3, "size": 1} == s[3].to_dict()
assert {"from": 3, "size": 1} == s[3][0].to_dict()
assert {"from": 8, "size": 0} == s[3][5].to_dict()


def test_search_to_dict():
Expand Down

0 comments on commit f8d0e9f

Please sign in to comment.