# How to know the unique count using an aggregated filter?

**URL:** <https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719>\
**Category:** Kibana\
**Created:** [December 5, 2019, 1:03pm UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719 "2019-12-05T13:03:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![badpenguin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badpenguin/32/58861_2.png) [@badpenguin](https://discuss.elastic.co/u/badpenguin)\
**Post date:** [December 5, 2019, 1:03pm UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/1 "2019-12-05T13:03:38Z")

</div>

I've data organized as rows with: user\_id, product\_id, etc.

How can i have a unique count of how many users purchased both product\_id=123 and product\_id=234 ?

I'was unable to figure out how to perform this kind of aggregation.

---

<div class="post-metadata">

**Author:** ![thekm1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thekm1/32/35926_2.png) [@thekm1](https://discuss.elastic.co/u/thekm1)\
**Post date:** [December 5, 2019, 1:35pm UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/2 "2019-12-05T13:35:49Z")

</div>

Hi @badpenguin  
It depends how your documents looks like. Can you provide the mapping and some example documents from that specific index?  
Thank you

---

<div class="post-metadata">

**Author:** ![badpenguin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badpenguin/32/58861_2.png) [@badpenguin](https://discuss.elastic.co/u/badpenguin)\
**Post date:** [December 5, 2019, 1:52pm UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/3 "2019-12-05T13:52:14Z")

</div>

The document is flat, no nested properties:  
{  
user\_id,  
product\_id,  
...  
}

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [December 5, 2019, 3:56pm UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/4 "2019-12-05T15:56:28Z")

</div>

Hi @badpenguin.

please try with below code;  
`{ "size" : 0, "query" : { "bool" : { "must" : [{ "term" : { "product_id.keyword" : { "value" : "123 ", "boost" : 1.0 } } }, { "term" : { "product_id.keyword" : { "value" : "234 ", "boost" : 1.0 } } }], "adjust_pure_negative" : true, "boost" : 1.0 } }, "_source" : false, "stored_fields" : "_none_", "aggregations" : { "groupby" : { "filters" : { "filters" : [{ "match_all" : { "boost" : 1.0 } }], "other_bucket" : false, "other_bucket_key" : "_other_" }, "aggregations" : { "25097" : { "filter" : { "exists" : { "field" : "username", "boost" : 1.0 } } } } } } }`

Thanks  
HadoopHelp

---

<div class="post-metadata">

**Author:** ![badpenguin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badpenguin/32/58861_2.png) [@badpenguin](https://discuss.elastic.co/u/badpenguin)\
**Post date:** [December 6, 2019, 11:10am UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/6 "2019-12-06T11:10:22Z")

</div>

@ramekshkr1994 thank you. Where should i put this code?

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [December 9, 2019, 8:31am UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/7 "2019-12-09T08:31:10Z")

</div>

Hi @badpenguin.

where you using this use case ?

you are using any apps or third party tools?

i shared this code just from postman.

you can implement this code as per your requirement.

Thanks  
HadoopHelp

---

<div class="post-metadata">

**Author:** ![badpenguin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badpenguin/32/58861_2.png) [@badpenguin](https://discuss.elastic.co/u/badpenguin)\
**Post date:** [December 9, 2019, 9:17am UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/8 "2019-12-09T09:17:55Z")

</div>

I need to create a widget with "visualize"

---

<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:** [January 6, 2020, 9:17am UTC](https://discuss.elastic.co/t/how-to-know-the-unique-count-using-an-aggregated-filter/210719/9 "2020-01-06T09:17:56Z")

</div>

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