Skip to content

Commit 50db902

Browse files
haynesgtmiguelgrinberg
authored andcommitted
Add support for script as a dict in update
1 parent 2e22e0a commit 50db902

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

elasticsearch_dsl/_async/document.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ async def update(
252252
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
253253
doc, setting doc_as_upsert to true will use the contents of doc as
254254
the upsert value
255+
:arg script: the source code of the script as a string, or a dictionary
256+
with script attributes to update.
255257
:arg return_doc_meta: set to ``True`` to return all metadata from the
256258
index API call instead of only the operation result
257259
@@ -268,11 +270,15 @@ async def update(
268270
body["upsert"] = upsert
269271

270272
if script:
271-
script = {"source": script}
273+
if isinstance(script, str):
274+
script = {"source": script}
272275
else:
273276
script = {"id": script_id}
274277

275-
script["params"] = fields
278+
if "params" not in script:
279+
script["params"] = fields
280+
else:
281+
script["params"].update(fields)
276282

277283
body["script"] = script
278284
body["scripted_upsert"] = scripted_upsert

elasticsearch_dsl/_sync/document.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ def update(
250250
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
251251
doc, setting doc_as_upsert to true will use the contents of doc as
252252
the upsert value
253+
:arg script: the source code of the script as a string, or a dictionary
254+
with script attributes to update.
253255
:arg return_doc_meta: set to ``True`` to return all metadata from the
254256
index API call instead of only the operation result
255257
@@ -266,11 +268,15 @@ def update(
266268
body["upsert"] = upsert
267269

268270
if script:
269-
script = {"source": script}
271+
if isinstance(script, str):
272+
script = {"source": script}
270273
else:
271274
script = {"id": script_id}
272275

273-
script["params"] = fields
276+
if "params" not in script:
277+
script["params"] = fields
278+
else:
279+
script["params"].update(fields)
274280

275281
body["script"] = script
276282
body["scripted_upsert"] = scripted_upsert

tests/test_integration/_async/test_document.py

+18
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,24 @@ async def test_update_script(async_write_client):
241241
assert w.views == 47
242242

243243

244+
@pytest.mark.asyncio
245+
async def test_update_script_with_dict(async_write_client):
246+
await Wiki.init()
247+
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
248+
await w.save()
249+
250+
await w.update(
251+
script={
252+
"source": "ctx._source.views += params.inc1 + params.inc2",
253+
"params": {"inc1": 2},
254+
"lang": "painless",
255+
},
256+
inc2=3,
257+
)
258+
w = await Wiki.get(id="elasticsearch-py")
259+
assert w.views == 47
260+
261+
244262
@pytest.mark.asyncio
245263
async def test_update_retry_on_conflict(async_write_client):
246264
await Wiki.init()

tests/test_integration/_sync/test_document.py

+18
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,24 @@ def test_update_script(write_client):
241241
assert w.views == 47
242242

243243

244+
@pytest.mark.sync
245+
def test_update_script_with_dict(write_client):
246+
Wiki.init()
247+
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
248+
w.save()
249+
250+
w.update(
251+
script={
252+
"source": "ctx._source.views += params.inc1 + params.inc2",
253+
"params": {"inc1": 2},
254+
"lang": "painless",
255+
},
256+
inc2=3,
257+
)
258+
w = Wiki.get(id="elasticsearch-py")
259+
assert w.views == 47
260+
261+
244262
@pytest.mark.sync
245263
def test_update_retry_on_conflict(write_client):
246264
Wiki.init()

0 commit comments

Comments
 (0)