Elasticsearch aggregate unique attribute per groupId

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