# Elasticsearch aggregate unique attribute per groupId

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregate-unique-attribute-per-groupid/36642>\
**Category:** Elasticsearch\
**Created:** [December 8, 2015, 2:25pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregate-unique-attribute-per-groupid/36642 "2015-12-08T14:25:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hasan\_Rahal](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hasan\_Rahal](https://discuss.elastic.co/u/Hasan_Rahal)\
**Post date:** [December 8, 2015, 2:25pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregate-unique-attribute-per-groupid/36642/1 "2015-12-08T14:25:38Z")

</div>

I have a separate doc in elasticsearch for each product.  
Each product has a **unique** _productId_ and a **non unique** _groupId_ along with other attributes eg: _categories_.

I want to be able to aggregate different attributes with their count per **unique** groupId

example:

doc 1:

```
{
  "productId": 123
  "groupId" xyz,
  "categories": [{"value": "shoes"}, {"value": "t-shirt"}]
}

```

doc 2:

```
{
  "productId": 345
  "groupId" xyz,
  "categories": [{"value": "shoes"}, {"value": "t-shirt"}]
}

```

doc 3:

```
{
  "productId": 456
  "groupId" abc,
  "categories": [{"value": "t-shirt"}]
}

```

doc 4:

```
{
  "productId": 567
  "groupId" abc,
  "categories": [{"value": "shoes"}, {"value": "makeup"}]
}

```

* * *

expected results, something like:

- shoes: 2
- t-shirt: 2
- makeup: 1

so I want to **count** each item once if it exists with the same _groupId_

---

<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:** [July 5, 2017, 11:32pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregate-unique-attribute-per-groupid/36642/2 "2017-07-05T23:32:37Z")

</div>


