# Aggregations and scripted expressions

**URL:** <https://discuss.elastic.co/t/aggregations-and-scripted-expressions/21235>\
**Category:** Elasticsearch\
**Created:** [December 12, 2014, 12:36pm UTC](https://discuss.elastic.co/t/aggregations-and-scripted-expressions/21235 "2014-12-12T12:36:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![msbreuer](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@msbreuer](https://discuss.elastic.co/u/msbreuer)\
**Post date:** [December 12, 2014, 12:36pm UTC](https://discuss.elastic.co/t/aggregations-and-scripted-expressions/21235/1 "2014-12-12T12:36:20Z")

</div>

I am building a date\_histogram query using aggregations. Per bucket are two  
different aggregations. First aggregations summarizes the number of  
activities and second one builds the sum over sum(duration per activity).

My Query looks like this:

"aggregations": {  
"over-time": {  
"date\_histogram": {  
"field": "timestamp",  
"interval": "1M"  
},  
"aggregations": {  
"activities": {  
"sum": {  
"field": "activity-count"  
}  
},  
"duration": {  
"sum": {  
"field": "activity-duration"  
}  
}  
}  
}  
}

Question: Required is a field containing the result of "activity-duration /  
activity-count".

I would think about a scripted expression, but how to use it here. With ES  
1.4 the scripted\_metrics was introduced (as experimental), but I think that  
is not what I am looking for.

Any idea?

regards,  
markus

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/984b97b5-4cfd-4393-a0ad-bb6bc6dd96f7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/984b97b5-4cfd-4393-a0ad-bb6bc6dd96f7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:43am UTC](https://discuss.elastic.co/t/aggregations-and-scripted-expressions/21235/2 "2017-07-06T00:43:54Z")

</div>


