Skip to content

Commit d0c2c9b

Browse files
Document.update() should accept fields set to None or empty (#1820) (#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 <[email protected]>
1 parent 94c8248 commit d0c2c9b

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-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: 20 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,19 @@ 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(refresh=True)
520+
await d.update(tags=[], refresh=True)
521+
assert d.tags == []
522+
523+
r = await Tags.search().execute()
524+
assert r.hits[0].tags == []
525+
526+
507527
@pytest.mark.asyncio
508528
async def test_save_automatically_uses_seq_no_and_primary_term(async_data_client):
509529
elasticsearch_repo = await Repository.get("elasticsearch-dsl-py")

tests/test_integration/_sync/test_document.py

Lines changed: 20 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,19 @@ 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(refresh=True)
514+
d.update(tags=[], refresh=True)
515+
assert d.tags == []
516+
517+
r = Tags.search().execute()
518+
assert r.hits[0].tags == []
519+
520+
501521
@pytest.mark.sync
502522
def test_save_automatically_uses_seq_no_and_primary_term(data_client):
503523
elasticsearch_repo = Repository.get("elasticsearch-dsl-py")

0 commit comments

Comments
 (0)