diff --git a/docs/reference/mapping/runtime.asciidoc b/docs/reference/mapping/runtime.asciidoc index 18fd806e64aae..03dea952f5d65 100644 --- a/docs/reference/mapping/runtime.asciidoc +++ b/docs/reference/mapping/runtime.asciidoc @@ -597,7 +597,6 @@ the values of runtime fields. Runtime fields won't display in `_source`, but the `fields` API works for all fields, even those that were not sent as part of the original `_source`. -[discrete] [[runtime-define-field-dayofweek]] ==== Define a runtime field to calculate the day of week For example, the following request adds a runtime field called `day_of_week`. @@ -626,7 +625,6 @@ PUT my-index-000001/ } ---- -[discrete] [[runtime-ingest-data]] ==== Ingest some data Let's ingest some sample data, which will result in two indexed fields: @@ -660,7 +658,6 @@ POST /my-index-000001/_bulk?refresh ---- //TEST[continued] -[discrete] [[runtime-search-dayofweek]] ==== Search for the calculated day of week The following request uses the search API to retrieve the `day_of_week` field diff --git a/docs/reference/mapping/types/flattened.asciidoc b/docs/reference/mapping/types/flattened.asciidoc index 8bf740977010d..82dd988aa99b0 100644 --- a/docs/reference/mapping/types/flattened.asciidoc +++ b/docs/reference/mapping/types/flattened.asciidoc @@ -191,6 +191,62 @@ POST my-index-000001/_search // TESTRESPONSE[s/"max_score" : 1.0/"max_score" : $body.hits.max_score/] // TESTRESPONSE[s/"_score" : 1.0/"_score" : $body.hits.hits.0._score/] +You can also use a <> to retrieve +values from sub-fields of flattened fields. Instead of including +`doc[''].value` in your Painless script, use +`doc['.'].value`. For example, if you have a +flattened field called `label` with a `release` sub-field, your Painless script +would be `doc['labels.release'].value`. + +For example, let's say your mapping contains two fields, one of which is of the +`flattened` type: + +[source,console] +---- +PUT my-index-000001 +{ + "mappings": { + "properties": { + "title": { + "type": "text" + }, + "labels": { + "type": "flattened" + } + } + } +} +---- + +Index a few documents containing your mapped fields. The `labels` field has +three sub-fields: + +[source,console] +---- +POST /my-index-000001/_bulk?refresh +{"index":{}} +{"title":"Something really urgent","labels":{"priority":"urgent","release":["v1.2.5","v1.3.0"],"timestamp":{"created":1541458026,"closed":1541457010}}} +{"index":{}} +{"title":"Somewhat less urgent","labels":{"priority":"high","release":["v1.3.0"],"timestamp":{"created":1541458026,"closed":1541457010}}} +{"index":{}} +{"title":"Not urgent","labels":{"priority":"low","release":["v1.2.0"],"timestamp":{"created":1541458026,"closed":1541457010}}} +---- +// TEST[continued] + +Because `labels` is a `flattened` field type, the entire object is mapped as a +single field. To retrieve values from this sub-field in a Painless script, use +the `doc['.'].value` format. + +[source,painless] +---- +"script": { + "source": """ + if (doc['labels.release'].value.equals('v1.3.0')) + {emit(doc['labels.release'].value)} + else{emit('Version mismatch')} + """ +---- + [[flattened-params]] ==== Parameters for flattened object fields