Skip to content

Commit c88ef94

Browse files
Document.update() should accept fields set to None or empty
Fixes #1819
1 parent be934a4 commit c88ef94

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

elasticsearch_dsl/_async/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ async def update(
295295
merge(self, fields)
296296

297297
# prepare data for ES
298-
values = self.to_dict()
298+
values = self.to_dict(skip_empty=False)
299299

300300
# if fields were given: partial update
301301
body["doc"] = {k: values.get(k) for k in fields.keys()}

elasticsearch_dsl/_sync/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def update(
293293
merge(self, fields)
294294

295295
# prepare data for ES
296-
values = self.to_dict()
296+
values = self.to_dict(skip_empty=False)
297297

298298
# if fields were given: partial update
299299
body["doc"] = {k: values.get(k) for k in fields.keys()}

tests/test_integration/_async/test_document.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ class Index:
120120
name = "test-serialization"
121121

122122

123+
class Tags(AsyncDocument):
124+
tags = Keyword(multi=True)
125+
126+
class Index:
127+
name = "tags"
128+
129+
123130
@pytest.mark.asyncio
124131
async def test_serialization(async_write_client):
125132
await SerializationDoc.init()
@@ -504,6 +511,26 @@ async def test_save_updates_existing_doc(async_data_client):
504511
assert new_repo["_seq_no"] == elasticsearch_repo.meta.seq_no
505512

506513

514+
@pytest.mark.asyncio
515+
async def test_update_empty_field(async_client):
516+
await Tags._index.delete(ignore_unavailable=True)
517+
await Tags.init()
518+
d = Tags(id="123", tags=["a", "b"])
519+
await d.save(wait_for_active_shards=1)
520+
await d.update(tags=[])
521+
assert d.tags == []
522+
523+
while True:
524+
try:
525+
r = await Tags.search().execute()
526+
d = r.hits[0]
527+
except IndexError:
528+
continue
529+
else:
530+
break
531+
assert d.tags == []
532+
533+
507534
@pytest.mark.asyncio
508535
async def test_save_automatically_uses_seq_no_and_primary_term(async_data_client):
509536
elasticsearch_repo = await Repository.get("elasticsearch-dsl-py")

tests/test_integration/_sync/test_document.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ class Index:
120120
name = "test-serialization"
121121

122122

123+
class Tags(Document):
124+
tags = Keyword(multi=True)
125+
126+
class Index:
127+
name = "tags"
128+
129+
123130
@pytest.mark.sync
124131
def test_serialization(write_client):
125132
SerializationDoc.init()
@@ -498,6 +505,26 @@ def test_save_updates_existing_doc(data_client):
498505
assert new_repo["_seq_no"] == elasticsearch_repo.meta.seq_no
499506

500507

508+
@pytest.mark.sync
509+
def test_update_empty_field(client):
510+
Tags._index.delete(ignore_unavailable=True)
511+
Tags.init()
512+
d = Tags(id="123", tags=["a", "b"])
513+
d.save(wait_for_active_shards=1)
514+
d.update(tags=[])
515+
assert d.tags == []
516+
517+
while True:
518+
try:
519+
r = Tags.search().execute()
520+
d = r.hits[0]
521+
except IndexError:
522+
continue
523+
else:
524+
break
525+
assert d.tags == []
526+
527+
501528
@pytest.mark.sync
502529
def test_save_automatically_uses_seq_no_and_primary_term(data_client):
503530
elasticsearch_repo = Repository.get("elasticsearch-dsl-py")

0 commit comments

Comments
 (0)