Skip to content

[DOCS] Retrieve values from flattened fields w/ runtime fields #73630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/reference/mapping/runtime.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions docs/reference/mapping/types/flattened.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<modules-scripting-painless,Painless script>> to retrieve
values from sub-fields of flattened fields. Instead of including
`doc['<field_name>'].value` in your Painless script, use
`doc['<field_name>.<sub-field_name>'].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['<field_name>.<sub-field_name>'].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

Expand Down