@@ -191,6 +191,62 @@ POST my-index-000001/_search
191
191
// TESTRESPONSE[s/"max_score" : 1.0/"max_score" : $body.hits.max_score/]
192
192
// TESTRESPONSE[s/"_score" : 1.0/"_score" : $body.hits.hits.0._score/]
193
193
194
+ You can also use a <<modules-scripting-painless,Painless script>> to retrieve
195
+ values from sub-fields of flattened fields. Instead of including
196
+ `doc['<field_name>'].value` in your Painless script, use
197
+ `doc['<field_name>.<sub-field_name>'].value`. For example, if you have a
198
+ flattened field called `label` with a `release` sub-field, your Painless script
199
+ would be `doc['labels.release'].value`.
200
+
201
+ For example, let's say your mapping contains two fields, one of which is of the
202
+ `flattened` type:
203
+
204
+ [source,console]
205
+ ----
206
+ PUT my-index-000001
207
+ {
208
+ "mappings": {
209
+ "properties": {
210
+ "title": {
211
+ "type": "text"
212
+ },
213
+ "labels": {
214
+ "type": "flattened"
215
+ }
216
+ }
217
+ }
218
+ }
219
+ ----
220
+
221
+ Index a few documents containing your mapped fields. The `labels` field has
222
+ three sub-fields:
223
+
224
+ [source,console]
225
+ ----
226
+ POST /my-index-000001/_bulk?refresh
227
+ {"index":{}}
228
+ {"title":"Something really urgent","labels":{"priority":"urgent","release":["v1.2.5","v1.3.0"],"timestamp":{"created":1541458026,"closed":1541457010}}}
229
+ {"index":{}}
230
+ {"title":"Somewhat less urgent","labels":{"priority":"high","release":["v1.3.0"],"timestamp":{"created":1541458026,"closed":1541457010}}}
231
+ {"index":{}}
232
+ {"title":"Not urgent","labels":{"priority":"low","release":["v1.2.0"],"timestamp":{"created":1541458026,"closed":1541457010}}}
233
+ ----
234
+ // TEST[continued]
235
+
236
+ Because `labels` is a `flattened` field type, the entire object is mapped as a
237
+ single field. To retrieve values from this sub-field in a Painless script, use
238
+ the `doc['<field_name>.<sub-field_name>'].value` format.
239
+
240
+ [source,painless]
241
+ ----
242
+ "script": {
243
+ "source": """
244
+ if (doc['labels.release'].value.equals('v1.3.0'))
245
+ {emit(doc['labels.release'].value)}
246
+ else{emit('Version mismatch')}
247
+ """
248
+ ----
249
+
194
250
[[flattened-params]]
195
251
==== Parameters for flattened object fields
196
252
0 commit comments