# Scriptet metric aggregation as sub aggregation after date histogram

**URL:** <https://discuss.elastic.co/t/scriptet-metric-aggregation-as-sub-aggregation-after-date-histogram/61579>\
**Category:** Elasticsearch\
**Created:** [September 27, 2016, 8:44am UTC](https://discuss.elastic.co/t/scriptet-metric-aggregation-as-sub-aggregation-after-date-histogram/61579 "2016-09-27T08:44:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michel\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michel_s/32/12170_2.png) [@Michel\_S](https://discuss.elastic.co/u/Michel_S)\
**Post date:** [September 27, 2016, 8:44am UTC](https://discuss.elastic.co/t/scriptet-metric-aggregation-as-sub-aggregation-after-date-histogram/61579/1 "2016-09-27T08:44:32Z")

</div>

Hi,  
I'm trying to use a scripted metric aggregation as sub aggregation after a date histogram aggregation and I'm getting some errors: 'Cannot invoke method getAt() on null object'

If I do this same scripted metric aggregation after another type of bucket aggregation (non date histogram) then everything works.

Does anybody know why this type of query is failing after a date histogram aggregation?

script:  
_{_  
\_ "query":{\_  
\_ "bool": {\_  
\_ "must": [\_  
\_ {"bool": {\_  
\_ "should": [\_  
\_ {"term": {"host": "pvx9alfcl1"}},\_  
\_ {"term": {"host": "pvx9alfcl2"}}\_  
\_ ]\_  
\_ }},\_  
\_ {"term": {"HTTP\_url": "download"}}\_  
\_ ]\_  
\_ }\_  
\_ },\_  
\_ "aggs": {\_  
\_ "requests\_over\_time" : {\_  
\_ "date\_histogram" : {\_  
\_ "field": "@timestamp",\_  
\_ "interval" : "1d"\_  
\_ },\_  
\_ "aggs": {\_  
\_ "profit": {\_  
\_ "scripted\_metric": {\_  
\_ "init\_script" : {\_  
\_ "file": "_script\_init\_01",_  
\_ "lang": "groovy"\_  
\_ },\_  
\_ "map\_script" : {\_  
\_ "file": "_script\_map\_01",_  
\_ "lang": "groovy"\_  
\_ },\_  
\_ "combine\_script" : {\_  
\_ "file": "_script\_combine\_01",_  
\_ "lang": "groovy"\_  
\_ },\_  
\_ "reduce\_script" : {\_  
\_ "file": "_script\_reduce\_01",_  
\_ "lang": "groovy"\_  
\_ }\_  
\_ }\_  
\_ }\_  
\_ }\_  
\_ }\_  
\_ }\_  
_}_

regards,  
Michel

---

<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 5, 2017, 10:16pm UTC](https://discuss.elastic.co/t/scriptet-metric-aggregation-as-sub-aggregation-after-date-histogram/61579/2 "2017-07-05T22:16:59Z")

</div>


