Best way to use range of user purchase with aggregation of all his purchases

Inside my document 'user' I have a field 'purchases' and my users have got a large amount of purchases. My problem is that I need to know which users have more than a certain number of purchases.

'purchases' is a nested field and inside all of them I've got a field called 'quantity' and I need to aggregate all of quantities of my users. That's how I'm trying to search them so far.

{
   "query":{
      "bool":{
         "must":[
            {
               "nested":{
                  "path":"purchases",
                  "query":{
                     "bool":{
                        "should":[
                           {
                              "range":{
                                 "purchases.quantity":{
                                    "gte":"1",
                                    "lte":"10"
                                 }
                              }
                           }
                        ],
                        "minimum_should_match":1
                     }
                  }
               }
            }
         ]
      }
   }
}

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