Skip to content

Commit e2c470a

Browse files
author
Adam Locke
authored
[DOCS] Retrieve values from flattened fields w/ runtime fields (#73630)
* [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 873095f commit e2c470a

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
@@ -597,7 +597,6 @@ the values of runtime fields. Runtime fields won't display in `_source`, but
597597
the `fields` API works for all fields, even those that were not sent as part of
598598
the original `_source`.
599599

600-
[discrete]
601600
[[runtime-define-field-dayofweek]]
602601
==== Define a runtime field to calculate the day of week
603602
For example, the following request adds a runtime field called `day_of_week`.
@@ -626,7 +625,6 @@ PUT my-index-000001/
626625
}
627626
----
628627

629-
[discrete]
630628
[[runtime-ingest-data]]
631629
==== Ingest some data
632630
Let's ingest some sample data, which will result in two indexed fields:
@@ -660,7 +658,6 @@ POST /my-index-000001/_bulk?refresh
660658
----
661659
//TEST[continued]
662660

663-
[discrete]
664661
[[runtime-search-dayofweek]]
665662
==== Search for the calculated day of week
666663
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
@@ -191,6 +191,62 @@ POST my-index-000001/_search
191191
// TESTRESPONSE[s/"max_score" : 1.0/"max_score" : $body.hits.max_score/]
192192
// TESTRESPONSE[s/"_score" : 1.0/"_score" : $body.hits.hits.0._score/]
193193

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

0 commit comments

Comments
 (0)