# Average of top n terms

**URL:** <https://discuss.elastic.co/t/average-of-top-n-terms/26165>\
**Category:** Elasticsearch\
**Created:** [July 23, 2015, 2:17pm UTC](https://discuss.elastic.co/t/average-of-top-n-terms/26165 "2015-07-23T14:17:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roel](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@roel](https://discuss.elastic.co/u/roel)\
**Post date:** [July 23, 2015, 2:18pm UTC](https://discuss.elastic.co/t/average-of-top-n-terms/26165/1 "2015-07-23T14:18:00Z")

</div>

Hallo,

In a certain index documents have a keyword, a rank and a timestamp. The rank for a keyword may differ from time to time. This means the dataset may look like this:

```
{"keywords": "piano", "rank" 1, "timestamp": 1437642812}
{"keywords": "piano", "rank" 2, "timestamp": 1437642813}
{"keywords": "electric guitar", "rank" 5, "timestamp": 1437644326}

```

I would like to get the average rank of the top 500 most occuring keywords. But I cannot find out how to do this. My current try-outs seem to always give the average for the entire dataset.

Roel

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 23, 2015, 2:29pm UTC](https://discuss.elastic.co/t/average-of-top-n-terms/26165/2 "2015-07-23T14:29:11Z")

</div>

In the current version this is not possible, but with pipeline aggregations coming in version 2.0 you will be able to use the `avg_bucket` aggregation to do this: [https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-pipeline-avg-bucket-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-pipeline-avg-bucket-aggregation.html)

In the mean time you would need to do an aggregation for the top 500 terms and perform the average calculation on the client side

---

<div class="post-metadata">

**Author:** ![roel](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@roel](https://discuss.elastic.co/u/roel)\
**Post date:** [July 23, 2015, 2:59pm UTC](https://discuss.elastic.co/t/average-of-top-n-terms/26165/3 "2015-07-23T14:59:41Z")

</div>

Thank you for your answer.  
I imagine this would work for a normal script, but is this also possible when I want to use the data for Kibana?

Roel

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 23, 2015, 3:19pm UTC](https://discuss.elastic.co/t/average-of-top-n-terms/26165/4 "2015-07-23T15:19:27Z")

</div>

Yes this would work in 2.0 for requests straight to Elasticsearch. However, it will take some time for the functionality to be added to the Kibana interface. It is something the Kibana team are thinking about how to add though

---

<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, 11:59pm UTC](https://discuss.elastic.co/t/average-of-top-n-terms/26165/5 "2017-07-05T23:59:29Z")

</div>


