Skip to content

Commit 09b6028

Browse files
authored
Add painless context examples for update and update-by-query (#37943)
This commit improves the example docs for contexts in painless. relates #34829
1 parent 49bd871 commit 09b6028

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

docs/build.gradle

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,17 +1145,23 @@ buildRestTests.setups['seats'] = '''
11451145
type: keyword
11461146
cost:
11471147
type: long
1148+
row:
1149+
type: long
1150+
number:
1151+
type: long
1152+
sold:
1153+
type: boolean
11481154
- do:
11491155
bulk:
11501156
index: seats
11511157
type: _doc
11521158
refresh: true
11531159
body: |
1154-
{"index":{}}
1155-
{"theatre": "Skyline", "cost": 1}
1156-
{"index":{}}
1157-
{"theatre": "Graye", "cost": 5}
1158-
{"index":{}}
1159-
{"theatre": "Graye", "cost": 8}
1160-
{"index":{}}
1161-
{"theatre": "Skyline", "cost": 10}'''
1160+
{"index":{"_id": "1"}}
1161+
{"theatre": "Skyline", "cost": 37, "row": 1, "number": 7, "sold": false}
1162+
{"index":{"_id": "2"}}
1163+
{"theatre": "Graye", "cost": 30, "row": 3, "number": 5, "sold": false}
1164+
{"index":{"_id": "3"}}
1165+
{"theatre": "Graye", "cost": 33, "row": 2, "number": 6, "sold": false}
1166+
{"index":{"_id": "4"}}
1167+
{"theatre": "Skyline", "cost": 20, "row": 5, "number": 2, "sold": false}'''

docs/painless/painless-contexts/painless-update-by-query-context.asciidoc

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,45 @@ result of query.
5151

5252
*API*
5353

54-
The standard <<painless-api-reference, Painless API>> is available.
54+
The standard <<painless-api-reference, Painless API>> is available.
55+
56+
*Example*
57+
58+
To run this example, first follow the steps in
59+
<<painless-context-examples, context examples>>.
60+
61+
The following query finds all seats in a specific section that have not been
62+
sold and lowers the price by 2:
63+
64+
[source,js]
65+
--------------------------------------------------
66+
POST /seats/_update_by_query
67+
{
68+
"query": {
69+
"bool": {
70+
"filter": [
71+
{
72+
"range": {
73+
"row": {
74+
"lte": 3
75+
}
76+
}
77+
},
78+
{
79+
"match": {
80+
"sold": false
81+
}
82+
}]
83+
}
84+
},
85+
"script": {
86+
"source": "ctx._source.cost -= params.discount",
87+
"lang": "painless",
88+
"params": {
89+
"discount": 2
90+
}
91+
}
92+
}
93+
--------------------------------------------------
94+
// CONSOLE
95+
// TEST[setup:seats]

docs/painless/painless-contexts/painless-update-context.asciidoc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,28 @@ add, modify, or delete fields within a single document.
5252

5353
*API*
5454

55-
The standard <<painless-api-reference, Painless API>> is available.
55+
The standard <<painless-api-reference, Painless API>> is available.
56+
57+
*Example*
58+
59+
To run this example, first follow the steps in
60+
<<painless-context-examples, context examples>>.
61+
62+
The following query updates a document to be sold, and sets the cost
63+
to the actual price paid after discounts:
64+
65+
[source,js]
66+
--------------------------------------------------
67+
POST /seats/_update/3
68+
{
69+
"script": {
70+
"source": "ctx._source.sold = true; ctx._source.cost = params.sold_cost",
71+
"lang": "painless",
72+
"params": {
73+
"sold_cost": 26
74+
}
75+
}
76+
}
77+
--------------------------------------------------
78+
// CONSOLE
79+
// TEST[setup:seats]

0 commit comments

Comments
 (0)