Skip to content

Commit

Permalink
Document.update() should accept fields set to None or empty
Browse files Browse the repository at this point in the history
Fixes #1819
  • Loading branch information
miguelgrinberg committed May 10, 2024
1 parent b5435a8 commit ce95ec2
Show file tree
Hide file tree
Showing 4 changed files with 56 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
27 changes: 27 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,26 @@ 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(wait_for_active_shards=1)
await d.update(tags=[])
assert d.tags == []

while True:
try:
r = await Tags.search().execute()
d = r.hits[0]
except IndexError:
continue
else:
break
assert d.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
27 changes: 27 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,26 @@ 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(wait_for_active_shards=1)
d.update(tags=[])
assert d.tags == []

while True:
try:
r = Tags.search().execute()
d = r.hits[0]
except IndexError:
continue
else:
break
assert d.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 ce95ec2

Please sign in to comment.