# Extended Stats Bucket - Using Top Hits Agg

**URL:** <https://discuss.elastic.co/t/extended-stats-bucket-using-top-hits-agg/184395>\
**Category:** Elasticsearch\
**Created:** [June 5, 2019, 2:48pm UTC](https://discuss.elastic.co/t/extended-stats-bucket-using-top-hits-agg/184395 "2019-06-05T14:48:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ek9boy](https://avatars.discourse-cdn.com/v4/letter/e/50afbb/32.png) [@ek9boy](https://discuss.elastic.co/u/ek9boy)\
**Post date:** [June 5, 2019, 2:48pm UTC](https://discuss.elastic.co/t/extended-stats-bucket-using-top-hits-agg/184395/1 "2019-06-05T14:48:34Z")

</div>

Hi,

I am struggling to use a "Top Hits" aggregation and "extended\_stats\_bucket".

From the buckets returned my "extended\_stats\_bucket" path cannot ready the metric value.

Is there a way to do this?

The image on the left is with the extended\_stats\_bucket code (This does not actually work - the image on the right is what i want to read. Is this possible?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/4/446147aec9c2e8186e330b1cf118ff1a14a8479c.png)

below is my query.

```
GET logstash-masspay_bam-sit-2019.06.03/_search
{
"size" : 0,
  "query": {
"bool": {
  "must": [
    {
      "range": {
        "@timestamp": {
          "gte": "now/d-30d",
          "lt": "now/d"
        }
      }
    },
    {
    "query_string": {
      "query": "((office.keyword:DE2 OR office.keyword:FR2 OR office.keyword:ES2 OR office.keyword: IE2 office.keyword:PT2 ) AND (!status.keyword:FXRATE OR !status.keyword:WAIT_FX_RATE)) OR (office.keyword:GB2 OR office.keyword:GB4)",
      "analyze_wildcard": true,
      "default_field": "*"
    }
  },
    {
      "terms" : {
        "sourcetype.keyword": [
          "mp_bam_queue",
          "mp_bam_queue_tot"
        ]
      }
    }
  ]    
}
  },
  "aggs" : {
"Interval_Office_Queue" : {
  "terms": {
    "size": 10000,
    "script": {
      "lang": "painless",
      "source": """
      def office=doc['office.keyword'].value;
      def queue=doc['status.keyword'].value;
      def hour=doc['@timestamp'].value.toString('HH');
      int quarter=Integer.parseInt(doc['@timestamp'].value.toString('mm'))/15;
        String minute='';
        if (quarter==0) {
          minute='00';
        } else if (quarter>0) {
          minute=String.valueOf(quarter*15);
        }
      return hour+minute+'_'+office+'_'+queue
      """
    }
  },
  "aggs": {
    "Interval_Office_Channel_Day": {
      "terms" : {
        "size": 30,
        "script" : {
          "lang": "painless",
          "source" : "String day=doc['@timestamp'].value.toString('YYYY-MM-dd'); return day"
          }
      },
                "aggs": {
        "CumulativeVolume": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": ["count_status"]
            },
            "sort": [
              {
                "@timestamp": {
                  "order": "desc"
                }
              }
            ]
          }
        },
        "CumulativeAmount": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": ["sum_base_amount"]
            },
            "sort": [
              {
                "@timestamp": {
                  "order": "desc"
                }
              }
            ]
          }
        },
    "ChannelVolumeStats": {
      "extended_stats_bucket": {
        "buckets_path": "Interval_Office_Channel_Day>CumulativeVolume"
      }
    },
    "ChannelAmountStats": {
      "extended_stats_bucket": {
        "buckets_path": "Interval_Office_Channel_Day>CumulativeAmount"
      }
    },
    "BucketMetadata": {
      "top_hits": {
        "size": 1,
        "_source": {
          "includes": ["office","office_country","status"]
        }
      }
    }                          
  }  
}
  }
}
}}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 3, 2019, 2:55pm UTC](https://discuss.elastic.co/t/extended-stats-bucket-using-top-hits-agg/184395/4 "2019-07-03T14:55:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
