Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch: access document nested value in groovy script

I have a document stored in ElasticSearch as below. _source:

 {
 "firstname": "John",
 "lastname": "Smith",
 "medals":[
           {
             "bucket": 100, 
             "count": 1
           },
           {
             "bucket": 150,
             "count": 2
           }
         ]
  }

I can access the string type value inside a document using doc.firstname for scripted metric aggregation http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html.

But I am not able to get the field value using doc.medals[0].bucket.

Can you please help me out and let me know how to access the values inside nested fields?

like image 625
Anil Kumar Avatar asked Jan 14 '15 05:01

Anil Kumar


People also ask

What is nested in elastic search?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What is nested document?

Embedded document or nested documents are those types of documents which contain a document inside another document.


1 Answers

Use _source for nested properties. Doc holds fields that are loaded in memory. Nested documents may not be loaded and should be accessed with _source.

For instance:

GET index/type
    {
     "aggs": {
      "NAME": {
      "scripted_metric": {
        "init_script": "_agg['collection']=[]",
        "map_script": "_agg['tr'].add(_source.propertry1.prop);",
        "combine_script": "return _agg",
        "reduce_script": "return _aggs"
      }
    }
  },
  "size": 0
}
like image 95
Aniket Avatar answered Nov 07 '22 12:11

Aniket