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

Inconsistent handling of empty lists in Document partial updates. #1819

Closed
albertisfu opened this issue May 7, 2024 · 2 comments · Fixed by #1820
Closed

Inconsistent handling of empty lists in Document partial updates. #1819

albertisfu opened this issue May 7, 2024 · 2 comments · Fixed by #1820
Labels
Category: Bug Something isn't right

Comments

@albertisfu
Copy link

Hi! At work we are currently developing an API that relies on Elasticsearch responses. Within our document mapping, we have defined multi-value fields, which we aim to represent as empty arrays ([]) in the API response when these fields are empty. When initially indexing the document, it successfully store empty lists into ES as [] by passing these fields with skip_empty=False.

However, we are encountering an issue with partial updates. After updating a field to become empty, it is stored as None instead of [], even though we are passing the field values as [].

The root cause appears to be within the current implementation of the Document.update method for partial document updates. This method prepares data for Elasticsearch by invoking the to_dict method, which, by default, ignores empty fields. Consequently, empty lists are treated as None and updated accordingly.

Proposed Fix:
To address this issue, I propose modifying the update method to accept skip_empty=False and passing this parameter to the to_dict method. This adjustment will ensure that empty lists are included in the update payload as empty lists instead of being interpreted as None.

Steps to Reproduce:

  1. Save a document with multi-value fields.
  2. Perform a partial update using the Document.update method, passing multi-value fields as empty lists [].
  3. Verify that the empty lists are updated as None instead of remaining as empty lists.

I am willing to contribute to fixing this issue if you believe it should be fixed in this way.

miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue May 8, 2024
@miguelgrinberg
Copy link
Collaborator

miguelgrinberg commented May 8, 2024

I was able to reproduce this easily. I think update() should always use to_dict(skip_empty=False), I don't see why you would send an empty value to update but would like it to be skipped. Working on a fix.

@albertisfu
Copy link
Author

You're right; there is no reason to pass skip_empty=True on an update. The default option, False, is correct.

Thank you!

miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue May 10, 2024
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this issue May 17, 2024
miguelgrinberg added a commit that referenced this issue May 20, 2024
* Document.update() should accept fields set to None or empty

Fixes #1819

* use refresh instead of wait loop
github-actions bot pushed a commit that referenced this issue May 20, 2024
* Document.update() should accept fields set to None or empty

Fixes #1819

* use refresh instead of wait loop

(cherry picked from commit 8e7b138)
miguelgrinberg added a commit that referenced this issue May 20, 2024
…1828)

* Document.update() should accept fields set to None or empty

Fixes #1819

* use refresh instead of wait loop

(cherry picked from commit 8e7b138)

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants