Explorar el Código

[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
Adam Locke hace 4 años
padre
commit
e2c470abed

+ 0 - 3
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

+ 56 - 0
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 <<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