10

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?

1 Answer 1

9

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.