Skip to content

Commit 19bcaff

Browse files
author
Adam Locke
authored
[DOCS] Retrieve values from flattened fields w/ runtime fields (#73630) (#73723)
* [DOCS] Add retriving from flattened fields * Clarify sub-field syntax * Moving sub-field retrieval to flattened field docs * Remove full example and de-emphasize runtime fields * Remove extraneous sample tag
1 parent 3cb60d5 commit 19bcaff

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

docs/reference/mapping/runtime.asciidoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,6 @@ the values of runtime fields. Runtime fields won't display in `_source`, but
605605
the `fields` API works for all fields, even those that were not sent as part of
606606
the original `_source`.
607607

608-
[discrete]
609608
[[runtime-define-field-dayofweek]]
610609
==== Define a runtime field to calculate the day of week
611610
For example, the following request adds a runtime field called `day_of_week`.
@@ -634,7 +633,6 @@ PUT my-index-000001/
634633
}
635634
----
636635

637-
[discrete]
638636
[[runtime-ingest-data]]
639637
==== Ingest some data
640638
Let's ingest some sample data, which will result in two indexed fields:
@@ -668,7 +666,6 @@ POST /my-index-000001/_bulk?refresh
668666
----
669667
//TEST[continued]
670668

671-
[discrete]
672669
[[runtime-search-dayofweek]]
673670
==== Search for the calculated day of week
674671
The following request uses the search API to retrieve the `day_of_week` field

docs/reference/mapping/types/flattened.asciidoc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,62 @@ POST my-index-000001/_search
192192
// TESTRESPONSE[s/"max_score" : 1.0/"max_score" : $body.hits.max_score/]
193193
// TESTRESPONSE[s/"_score" : 1.0/"_score" : $body.hits.hits.0._score/]
194194

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+
195251
[[flattened-params]]
196252
==== Parameters for flattened object fields
197253

0 commit comments

Comments
 (0)