Elasticsearch aggregation bucket filter


(Pavan Kumar) #1

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"
        }
     }
  }
}

(system) #2

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