File tree 4 files changed +52
-4
lines changed
4 files changed +52
-4
lines changed Original file line number Diff line number Diff line change @@ -252,6 +252,8 @@ async def update(
252
252
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
253
253
doc, setting doc_as_upsert to true will use the contents of doc as
254
254
the upsert value
255
+ :arg script: the source code of the script as a string, or a dictionary
256
+ with script attributes to update.
255
257
:arg return_doc_meta: set to ``True`` to return all metadata from the
256
258
index API call instead of only the operation result
257
259
@@ -268,11 +270,15 @@ async def update(
268
270
body ["upsert" ] = upsert
269
271
270
272
if script :
271
- script = {"source" : script }
273
+ if isinstance (script , str ):
274
+ script = {"source" : script }
272
275
else :
273
276
script = {"id" : script_id }
274
277
275
- script ["params" ] = fields
278
+ if "params" not in script :
279
+ script ["params" ] = fields
280
+ else :
281
+ script ["params" ].update (fields )
276
282
277
283
body ["script" ] = script
278
284
body ["scripted_upsert" ] = scripted_upsert
Original file line number Diff line number Diff line change @@ -250,6 +250,8 @@ def update(
250
250
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
251
251
doc, setting doc_as_upsert to true will use the contents of doc as
252
252
the upsert value
253
+ :arg script: the source code of the script as a string, or a dictionary
254
+ with script attributes to update.
253
255
:arg return_doc_meta: set to ``True`` to return all metadata from the
254
256
index API call instead of only the operation result
255
257
@@ -266,11 +268,15 @@ def update(
266
268
body ["upsert" ] = upsert
267
269
268
270
if script :
269
- script = {"source" : script }
271
+ if isinstance (script , str ):
272
+ script = {"source" : script }
270
273
else :
271
274
script = {"id" : script_id }
272
275
273
- script ["params" ] = fields
276
+ if "params" not in script :
277
+ script ["params" ] = fields
278
+ else :
279
+ script ["params" ].update (fields )
274
280
275
281
body ["script" ] = script
276
282
body ["scripted_upsert" ] = scripted_upsert
Original file line number Diff line number Diff line change @@ -241,6 +241,24 @@ async def test_update_script(async_write_client):
241
241
assert w .views == 47
242
242
243
243
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
+
244
262
@pytest .mark .asyncio
245
263
async def test_update_retry_on_conflict (async_write_client ):
246
264
await Wiki .init ()
Original file line number Diff line number Diff line change @@ -241,6 +241,24 @@ def test_update_script(write_client):
241
241
assert w .views == 47
242
242
243
243
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
+
244
262
@pytest .mark .sync
245
263
def test_update_retry_on_conflict (write_client ):
246
264
Wiki .init ()
You can’t perform that action at this time.
0 commit comments