Skip to content

Commit

Permalink
Add support for script as a dict in update (elastic#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
haynesgt authored Apr 24, 2024
1 parent 2e22e0a commit f4f6f13
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
10 changes: 8 additions & 2 deletions elasticsearch_dsl/_async/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ async def update(
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
doc, setting doc_as_upsert to true will use the contents of doc as
the upsert value
:arg script: the source code of the script as a string, or a dictionary
with script attributes to update.
:arg return_doc_meta: set to ``True`` to return all metadata from the
index API call instead of only the operation result
Expand All @@ -268,11 +270,15 @@ async def update(
body["upsert"] = upsert

if script:
script = {"source": script}
if isinstance(script, str):
script = {"source": script}
else:
script = {"id": script_id}

script["params"] = fields
if "params" not in script:
script["params"] = fields
else:
script["params"].update(fields)

body["script"] = script
body["scripted_upsert"] = scripted_upsert
Expand Down
10 changes: 8 additions & 2 deletions elasticsearch_dsl/_sync/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ def update(
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
doc, setting doc_as_upsert to true will use the contents of doc as
the upsert value
:arg script: the source code of the script as a string, or a dictionary
with script attributes to update.
:arg return_doc_meta: set to ``True`` to return all metadata from the
index API call instead of only the operation result
Expand All @@ -266,11 +268,15 @@ def update(
body["upsert"] = upsert

if script:
script = {"source": script}
if isinstance(script, str):
script = {"source": script}
else:
script = {"id": script_id}

script["params"] = fields
if "params" not in script:
script["params"] = fields
else:
script["params"].update(fields)

body["script"] = script
body["scripted_upsert"] = scripted_upsert
Expand Down
18 changes: 18 additions & 0 deletions tests/test_integration/_async/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ async def test_update_script(async_write_client):
assert w.views == 47


@pytest.mark.asyncio
async def test_update_script_with_dict(async_write_client):
await Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
await w.save()

await w.update(
script={
"source": "ctx._source.views += params.inc1 + params.inc2",
"params": {"inc1": 2},
"lang": "painless",
},
inc2=3,
)
w = await Wiki.get(id="elasticsearch-py")
assert w.views == 47


@pytest.mark.asyncio
async def test_update_retry_on_conflict(async_write_client):
await Wiki.init()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_integration/_sync/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ def test_update_script(write_client):
assert w.views == 47


@pytest.mark.sync
def test_update_script_with_dict(write_client):
Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
w.save()

w.update(
script={
"source": "ctx._source.views += params.inc1 + params.inc2",
"params": {"inc1": 2},
"lang": "painless",
},
inc2=3,
)
w = Wiki.get(id="elasticsearch-py")
assert w.views == 47


@pytest.mark.sync
def test_update_retry_on_conflict(write_client):
Wiki.init()
Expand Down

0 comments on commit f4f6f13

Please sign in to comment.