Skip to content

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

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

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elasticsearch_dsl/_async/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ async def update(
merge(self, fields)

# prepare data for ES
values = self.to_dict()
values = self.to_dict(skip_empty=False)

# if fields were given: partial update
body["doc"] = {k: values.get(k) for k in fields.keys()}
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch_dsl/_sync/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def update(
merge(self, fields)

# prepare data for ES
values = self.to_dict()
values = self.to_dict(skip_empty=False)

# if fields were given: partial update
body["doc"] = {k: values.get(k) for k in fields.keys()}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_integration/_async/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ class Index:
name = "test-serialization"


class Tags(AsyncDocument):
tags = Keyword(multi=True)

class Index:
name = "tags"


@pytest.mark.asyncio
async def test_serialization(async_write_client):
await SerializationDoc.init()
Expand Down Expand Up @@ -504,6 +511,19 @@ async def test_save_updates_existing_doc(async_data_client):
assert new_repo["_seq_no"] == elasticsearch_repo.meta.seq_no


@pytest.mark.asyncio
async def test_update_empty_field(async_client):
await Tags._index.delete(ignore_unavailable=True)
await Tags.init()
d = Tags(id="123", tags=["a", "b"])
await d.save(refresh=True)
await d.update(tags=[], refresh=True)
assert d.tags == []

r = await Tags.search().execute()
assert r.hits[0].tags == []


@pytest.mark.asyncio
async def test_save_automatically_uses_seq_no_and_primary_term(async_data_client):
elasticsearch_repo = await Repository.get("elasticsearch-dsl-py")
Expand Down
20 changes: 20 additions & 0 deletions tests/test_integration/_sync/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ class Index:
name = "test-serialization"


class Tags(Document):
tags = Keyword(multi=True)

class Index:
name = "tags"


@pytest.mark.sync
def test_serialization(write_client):
SerializationDoc.init()
Expand Down Expand Up @@ -498,6 +505,19 @@ def test_save_updates_existing_doc(data_client):
assert new_repo["_seq_no"] == elasticsearch_repo.meta.seq_no


@pytest.mark.sync
def test_update_empty_field(client):
Tags._index.delete(ignore_unavailable=True)
Tags.init()
d = Tags(id="123", tags=["a", "b"])
d.save(refresh=True)
d.update(tags=[], refresh=True)
assert d.tags == []

r = Tags.search().execute()
assert r.hits[0].tags == []


@pytest.mark.sync
def test_save_automatically_uses_seq_no_and_primary_term(data_client):
elasticsearch_repo = Repository.get("elasticsearch-dsl-py")
Expand Down
Loading