Skip to content

Commit 911aeed

Browse files
JeffSaxeVAjavanna
authored andcommitted
[DOCS] Add "remove a tag" script logic as an example (#32556)
It took me quite a while of online searching and experimenting to realize the function-call asymmetry in the Add versus Remove from a list, like the "tags" list! I realize we cannot give examples for every single thing the user wants to do in Painless, but this is such a common use case (removing a tag from a single doc, or from a set of docs with Update-By-Query) that I believe it ought to be demonstrated immediately after the "add a tag" example. We have an example of removing an entire document field, but not removing one element of a list (a multi-valued field). Also, a minor grammar fix: I have added an apostrophe to the word "its" in the accompanying text of the example just above.
1 parent eb41dc4 commit 911aeed

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

docs/reference/docs/update.asciidoc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ POST test/_doc/1/_update
4747
// TEST[continued]
4848

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

5252
[source,js]
5353
--------------------------------------------------
@@ -65,6 +65,28 @@ POST test/_doc/1/_update
6565
// CONSOLE
6666
// TEST[continued]
6767

68+
We can remove a tag from the list of tags. Note that the Painless function to
69+
`remove` a tag takes as its parameter the array index of the element you wish
70+
to remove, so you need a bit more logic to locate it while avoiding a runtime
71+
error. Note that if the tag was present more than once in the list, this will
72+
remove only one occurrence of it:
73+
74+
[source,js]
75+
--------------------------------------------------
76+
POST test/_doc/1/_update
77+
{
78+
"script" : {
79+
"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
80+
"lang": "painless",
81+
"params" : {
82+
"tag" : "blue"
83+
}
84+
}
85+
}
86+
--------------------------------------------------
87+
// CONSOLE
88+
// TEST[continued]
89+
6890
In addition to `_source`, the following variables are available through
6991
the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`
7092
and `_now` (the current timestamp).
@@ -172,7 +194,7 @@ the request was ignored.
172194
"_index": "test",
173195
"_type": "_doc",
174196
"_id": "1",
175-
"_version": 6,
197+
"_version": 7,
176198
"result": "noop"
177199
}
178200
--------------------------------------------------

0 commit comments

Comments
 (0)