|
@@ -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
|
|
|
|