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

**URL:** <https://discuss.elastic.co/t/best-way-to-use-range-of-user-purchase-with-aggregation-of-all-his-purchases/151268>\
**Category:** Elasticsearch\
**Created:** [October 5, 2018, 6:12pm UTC](https://discuss.elastic.co/t/best-way-to-use-range-of-user-purchase-with-aggregation-of-all-his-purchases/151268 "2018-10-05T18:12:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ygor\_Fraga](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ygor_fraga/32/36201_2.png) [@Ygor\_Fraga](https://discuss.elastic.co/u/Ygor_Fraga)\
**Post date:** [October 5, 2018, 6:12pm UTC](https://discuss.elastic.co/t/best-way-to-use-range-of-user-purchase-with-aggregation-of-all-his-purchases/151268/1 "2018-10-05T18:12:58Z")

</div>

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
                     }
                  }
               }
            }
         ]
      }
   }
}
```

---

<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 2, 2018, 6:23pm UTC](https://discuss.elastic.co/t/best-way-to-use-range-of-user-purchase-with-aggregation-of-all-his-purchases/151268/2 "2018-11-02T18:23:28Z")

</div>

Closed on November 2, 2018, 6:23pm UTC

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