# Elasticsearch aggregation bucket filter

**URL:** https://discuss.elastic.co/t/elasticsearch-aggregation-bucket-filter/152275
**Category:** Elasticsearch
**Created:** [October 12, 2018, 4:56pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-bucket-filter/152275 "2018-10-12T16:56:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![pavanputhra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pavanputhra/32/36484_2.png) [@pavanputhra](https://discuss.elastic.co/u/pavanputhra)
#### Post date: [October 12, 2018, 4:56pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-bucket-filter/152275/1 "2018-10-12T16:56:01Z")

</div>

Let's say I have following documents.

```
	{ productId: 1, month: 'oct', sales: 3455 }
{ productId: 1, month: 'nov', sales: 123 }
{ productId: 1, month: 'dec', sales: 34213 }

{ productId: 2, month: 'oct', sales: 342 }
{ productId: 2, month: 'nov', sales: 1231 }
{ productId: 2, month: 'dec', sales: 232 }

```

I am trying to figure out the Elasticsearch query for the following aggregation.

Query: Group By productId, find max sales per group, only for those group where the sale of month nov is greater than 1000 . (If sale of month nov is less than 100 whole bucket should not be included.

I am able to design a query for the `Group By productId, find max sales per group`

But unable to figure out how to filter the group (bucket) where sales of month nov are greater than 1000.

Following is query without bucket filter.

```
{
  "size":0,
  "query":{
     "bool":{
        "must":[
           {
              "terms":{
                 "month":["oct", "nov", "dec"]
              }
           }
        ]
     }
  },
  "aggs":{
     "byProductId":{
        "terms":{
           "field":"productId",
           "size":1000
        },
        "aggs":{
           "maxSales":{
              "max":{
                 "field":"sales"
              }
           }
        }
     },
     "count":{
        "cardinality":{
           "field":"productId"
        }
     }
  }
}
```

---

<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: [November 9, 2018, 5:09pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-bucket-filter/152275/2 "2018-11-09T17:09:17Z")

</div>

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