Skip to content

Add "remove a tag" script logic as an example #32556

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 5 commits into from
Aug 17, 2018
Merged
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
26 changes: 24 additions & 2 deletions docs/reference/docs/update.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ POST test/_doc/1/_update
// TEST[continued]

We can add a tag to the list of tags (note, if the tag exists, it
will still add it, since its a list):
will still add it, since it's a list):

[source,js]
--------------------------------------------------
Expand All @@ -65,6 +65,28 @@ POST test/_doc/1/_update
// CONSOLE
// TEST[continued]

We can remove a tag from the list of tags. Note that the Painless function to
`remove` a tag takes as its parameter the array index of the element you wish
to remove, so you need a bit more logic to locate it while avoiding a runtime
error. Note that if the tag was present more than once in the list, this will
remove only one occurrence of it:

[source,js]
--------------------------------------------------
POST test/_doc/1/_update
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[continued]

In addition to `_source`, the following variables are available through
the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`
and `_now` (the current timestamp).
Expand Down Expand Up @@ -172,7 +194,7 @@ the request was ignored.
"_index": "test",
"_type": "_doc",
"_id": "1",
"_version": 6,
"_version": 7,
"result": "noop"
}
--------------------------------------------------
Expand Down