# How can I limit bucket to n results for averaging?

**URL:** <https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432>\
**Category:** Elasticsearch\
**Created:** [September 16, 2015, 5:44pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432 "2015-09-16T17:44:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samthurston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samthurston/32/4812_2.png) [@samthurston](https://discuss.elastic.co/u/samthurston)\
**Post date:** [September 16, 2015, 5:44pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/1 "2015-09-16T17:44:19Z")

</div>

```
In the guide I saw an example: 

GET /cars/transactions/_search?search_type=count
{
   "query":{
      "match": {
         "make": "ford"
      }
   },
   "aggs":{
      "recent_sales": {
         "filter": { 
            "range": {
               "sold": {
                  "from": "now-1M"
               }
            }
         },
         "aggs": {
            "average_price":{
               "avg": {
                  "field": "price" 
               }
            }
         }
      }
   }
}

```

This is pretty close to what I want to do, but instead of a range of dates, I just want the _5 most recent_ results in each bucket, and I want to be able to average the price.

I have tried the limit filter in a bucket, but that doesn't do what I would expect it to do (documents per shard? really?) and it's deprecated anyway.

I also tried to use the `top_hits` aggregation but that won't allow me to average the results because it doesn't accept sub-aggregations.

I have tried to apply multiple filters, set size options, and all other sorts of things with no correct results. the closest I seem to be able to find to this problem is

[Top N documents with multiple buckets](https://discuss.elastic.co/t/top-n-documents-with-multiple-buckets/28354)

But I don't actually understand what he's doing and I don't think the `terms` aggregator applies since i'm trying to sort and limit by timestamps of which there are many many duplicates. I'm under some time pressure here and it seems like if I was doing this with a traditional SQL query I'd have been done yesterday. Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![Pieter\_Agenbag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pieter_agenbag/32/4562_2.png) [@Pieter\_Agenbag](https://discuss.elastic.co/u/Pieter_Agenbag)\
**Post date:** [September 17, 2015, 7:10am UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/2 "2015-09-17T07:10:59Z")

</div>

Hi.

> [@samthurston](#):
>
> But I don't actually understand what he's doing and I don't think the terms aggregator applies since i'm trying to sort and limit by timestamps of which there are many many duplicates

I dont think I understand what You are trying to do 😉

You want an avg aggregation , but avg of which buckets ,because you siad you Dont want a date range ?  
So avg of each unique timestamp ?

I dont have an answer for you , but maybe have a look at the [aggregation order](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order) , you can order a bucket by a field or metric other than the bucket itself.  
? Get the avg , but order by timestamp ?

---

<div class="post-metadata">

**Author:** ![samthurston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samthurston/32/4812_2.png) [@samthurston](https://discuss.elastic.co/u/samthurston)\
**Post date:** [September 17, 2015, 1:33pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/3 "2015-09-17T13:33:37Z")

</div>

> [@Pieter\_Agenbag](#):
>
> I dont think I understand what You are trying to do

I think the best way to put it is, I want the average price of the last 5 sales in each bucket. some of those sales could be years old so I can't define a time range to guarantee 5 sales.

I don't think the terms agg will work because neither my buckets nor my prices or timestamps are "terms" in that sense. the buckets are geographic regions, and the `geo_bounds` agg doesn't allow you to set per-bucket limits as far as i can tell. my problem isn't so much getting them ordered correctly by date, as limiting the count that `avg` considers to only 5 per bucket

---

<div class="post-metadata">

**Author:** ![Pieter\_Agenbag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pieter_agenbag/32/4562_2.png) [@Pieter\_Agenbag](https://discuss.elastic.co/u/Pieter_Agenbag)\
**Post date:** [September 17, 2015, 2:24pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/4 "2015-09-17T14:24:12Z")

</div>

Ah , I see.  
I dont have a solution for that.. but will give it some thought.  
Sounds like something you need ES 2.0's pipeline aggregations for.

If you Do find a solution, please post it here - sounds like a really useful thing to be able to do

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 17, 2015, 4:11pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/5 "2015-09-17T16:11:56Z")

</div>

In 2.0 (currently in beta) you can use the sampler agg to limit any child aggs (e.g. your avg) to the top N scoring docs:

[https://www.elastic.co/guide/en/elasticsearch/reference/2.0/search-aggregations-bucket-sampler-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/search-aggregations-bucket-sampler-aggregation.html)

---

<div class="post-metadata">

**Author:** ![samthurston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samthurston/32/4812_2.png) [@samthurston](https://discuss.elastic.co/u/samthurston)\
**Post date:** [September 21, 2015, 6:04pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/6 "2015-09-21T18:04:25Z")

</div>

> [@Mark\_Harwood](#):
>
> In 2.0 (currently in beta) you can use the sampler agg to limit any child aggs (e.g. your avg) to the top N scoring docs:

That actually works pretty well, provided I want more docs than I have shards, which isn't necessarily the case. But this will have to do. Thanks.

---

<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:48pm UTC](https://discuss.elastic.co/t/how-can-i-limit-bucket-to-n-results-for-averaging/29432/7 "2017-07-05T23:48:59Z")

</div>


