# Nested Aggregation, count of parents

**URL:** <https://discuss.elastic.co/t/nested-aggregation-count-of-parents/92620>\
**Category:** Elasticsearch\
**Created:** [July 11, 2017, 10:06am UTC](https://discuss.elastic.co/t/nested-aggregation-count-of-parents/92620 "2017-07-11T10:06:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_G](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Ben\_G](https://discuss.elastic.co/u/Ben_G)\
**Post date:** [July 11, 2017, 10:06am UTC](https://discuss.elastic.co/t/nested-aggregation-count-of-parents/92620/1 "2017-07-11T10:06:29Z")

</div>

Hi,

Say I have a schema:-

```
 {
"product": {
"mappings": {
  "main": {
    "properties": {
        
      ...
        
      "skus": {
        "type": "nested",
        "properties": {
            
          ...
            
          "attributes": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "value": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
}
}

```

So a product has sku's and sku's have attributes (key/value).

I can get the aggregation of attribute keys with the values for a product like so:-

```
"aggs": {
"attributes": {
  "nested": {
    "path": "skus.attributes"
  },
  "aggs": {
    "attribute": {
      "terms": {
        "field": "skus.attributes.key.keyword"
      },
      "aggs": {
        "value": {
          "terms": {
            "field": "skus.attributes.value.keyword"
          }
        }
      }
    }
  }
}
}

```

but this gives me the counts of sku's, rather than products:-

```
{
"aggregations": {
"attributes": {
  "doc_count": 20,
  "attribute": {
    ...
    "buckets": [
      {
        "key": "foo",
        "doc_count": 20,
        "value": {
          ...
          "buckets": [
            {
              "key": "bar",
              "doc_count": 10
            },
            {
              "key": "barr",
              "doc_count": 10
            }
          ]
        }
      }
    ]
  }
}
}
}

```

I also need to be able to filter the aggregates by an 'attribute'. ie, Get the attributes and their values where a product has a attribute 'size' and value 'medium'.

Is this possible?

Any help/guidance would be much appreciated.

Thanks  
Ben

---

<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:** [August 8, 2017, 10:06am UTC](https://discuss.elastic.co/t/nested-aggregation-count-of-parents/92620/2 "2017-08-08T10:06:41Z")

</div>

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