Skip to content

Add support for script as a dict in update #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading