Skip to content

Commit

Permalink
Document.update() should accept fields set to None or empty (#1820)
Browse files Browse the repository at this point in the history
* Document.update() should accept fields set to None or empty

Fixes #1819

* use refresh instead of wait loop
  • Loading branch information
miguelgrinberg authored May 20, 2024
1 parent be934a4 commit 8e7b138
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
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

0 comments on commit 8e7b138

Please sign in to comment.