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