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

Added support for slicing multiple times in Search class #1771

Merged
merged 7 commits into from
May 16, 2024

Conversation

miguelgrinberg
Copy link
Collaborator

@miguelgrinberg miguelgrinberg commented Apr 9, 2024

Fixes #799

This PR implements Pythonic slicing for the Search object.

Note: due to introducing backward incompatibilities, this change is scheduled to be incorporated in release 8.15. Starting with release 8.13.1 a warning will alert developers slicing results multiple times that this breaking change is coming.

@miguelgrinberg miguelgrinberg requested a review from pquentin April 9, 2024 18:45
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The tests make sense, but the code is difficult to read for me, so I asked clarification questions.

elasticsearch_dsl/search_base.py Show resolved Hide resolved
elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
@miguelgrinberg
Copy link
Collaborator Author

@pquentin I have simplified the slicing even more. If the from or the size aren't specified in the slicing, then they are not included in the final request body. So for example:

  • [4:] renders to {"from": 4}
  • [:4] renders to {"size": 4}
  • [2:4] renders to {"from": 2, "size": 2}
  • [:] renders to {}

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The logic looks good. It just needs documentation.

My only remaining worry is that this is a breaking change. Would it be useful to warn about using multiple slices in 8.13.1 and 8.14 and then switch to the new behavior in 8.15? Some users do pay attention to our warnings: elastic/elasticsearch-py#2518 (comment).

elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
elasticsearch_dsl/search_base.py Outdated Show resolved Hide resolved
elasticsearch_dsl/search_base.py Show resolved Hide resolved
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM. We'll have to make sure to label this as a breaking change in the release notes.

@miguelgrinberg miguelgrinberg added the backport 8.x Backport to 8.x label May 16, 2024
@miguelgrinberg miguelgrinberg merged commit fb57759 into elastic:main May 16, 2024
17 checks passed
@miguelgrinberg miguelgrinberg deleted the multi-slicing branch May 16, 2024 22:45
github-actions bot pushed a commit that referenced this pull request 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 pull request 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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.x Backport to 8.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slicing twice doesn't work
2 participants