Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slicing twice doesn't work #799

Closed
konstin opened this issue Dec 31, 2017 · 4 comments · Fixed by #1771
Closed

Slicing twice doesn't work #799

konstin opened this issue Dec 31, 2017 · 4 comments · Fixed by #1771
Labels
Category: Bug Something isn't right Priority: High

Comments

@konstin
Copy link
Contributor

konstin commented Dec 31, 2017

Iterators are expected to allow slicing twice, i.e.

>>> list(range(0,100)[10:15])
[10, 11, 12, 13, 14]
>>> list(range(0,100)[10:][:5])
[10, 11, 12, 13, 14]

This means essentially that [a:a+b] equals [a:][:b]

With a Search object slicing twice doesn't work:

>>> Search()[10:15].to_dict()
{'from': 10, 'query': {'match_all': {}}, 'size': 5}
>>> Search()[10:][:5].to_dict()
{'from': 0, 'query': {'match_all': {}}, 'size': 5}

(Sorry for submitting an incomplete issue first, I accidentally hit the wrong key)

@konstin konstin changed the title Slicing twice give leads to Slicing twice doesn't work Dec 31, 2017
konstin added a commit to meine-stadt-transparent/meine-stadt-transparent that referenced this issue Dec 31, 2017
@lolletsoc
Copy link

Hey @konstin,

I've created a branch in my fork with minor changes to support this. Take a look at the tests and let me know - specifically this one.

@honzakral, what do you think?

@konstin
Copy link
Contributor Author

konstin commented Jan 1, 2018

Hi @lolletsoc, the changes does solve the issue with slicing twice.

The behavior for slicing multiple times still doesn't match the expected pythonic behavior though. Even though I doubt anyone uses this in practice, it might be worth fixing it,

>>> list(range(0, 100)[10:][:5][5:])
[]
>>> Search()[10:][:5][5:].to_dict()
{'query': {'match_all': {}}, 'from': 15, 'size': 10}

@lolletsoc
Copy link

Yeah, exactly why I pointed you at that test. If we try to mold this around the pythonic way, we're going to end up with {'query': {'match_all': {}}, 'from': 0, 'size': 0}. Will wait on @honzakral's reponse - maybe he has some insights.

@honzakral
Copy link
Contributor

Thanks for raising this issue @konstin, I understand the confusion. Right now any slicing will reset the from/size combination which may be unexpected. What people might be looking for is additive slicing - further limiting the scope, what python's lists do and even what django's querysets do (0).

That sounds very reasonable to me and I think we should do it, we just need to be careful about documenting it properly since it is a breaking change.

Thanks again for bringing it up!

0 - https://github.com/django/django/blob/master/django/db/models/sql/query.py#L1603-L1624

@sethmlarson sethmlarson added the good first issue Good for new contributors label Jun 15, 2020
@sethmlarson sethmlarson added the v8.x Targeting major version 8 label Aug 10, 2020
@miguelgrinberg miguelgrinberg added the Category: Bug Something isn't right label Apr 5, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue Apr 9, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue Apr 9, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue Apr 9, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue Apr 9, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue Apr 9, 2024
@miguelgrinberg miguelgrinberg added Priority: High and removed v8.x Targeting major version 8 good first issue Good for new contributors labels Apr 26, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue May 16, 2024
miguelgrinberg added a commit that referenced this issue May 16, 2024
* Added support for slicing multiple times in Search class

Fixes #799

* simplify slicing logic

* A few more slicing unit tests

* Removed unnecessary comment

* simplified slicing logic even more

* added suggested changes

* add more slicing examples to documentation
github-actions bot pushed a commit that referenced this issue May 16, 2024
* Added support for slicing multiple times in Search class

Fixes #799

* simplify slicing logic

* A few more slicing unit tests

* Removed unnecessary comment

* simplified slicing logic even more

* added suggested changes

* add more slicing examples to documentation

(cherry picked from commit fb57759)
pquentin pushed a commit that referenced this issue May 17, 2024
* Added support for slicing multiple times in Search class

Fixes #799

* simplify slicing logic

* A few more slicing unit tests

* Removed unnecessary comment

* simplified slicing logic even more

* added suggested changes

* add more slicing examples to documentation

(cherry picked from commit fb57759)

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Bug Something isn't right Priority: High
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants