# Aggregate doc\_count per document and not per nested item?

**URL:** https://discuss.elastic.co/t/aggregate-doc-count-per-document-and-not-per-nested-item/214416
**Category:** Elasticsearch
**Created:** [January 9, 2020, 10:38am UTC](https://discuss.elastic.co/t/aggregate-doc-count-per-document-and-not-per-nested-item/214416 "2020-01-09T10:38:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Laurens\_Mertens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laurens_mertens/32/52225_2.png) [@Laurens\_Mertens](https://discuss.elastic.co/u/Laurens_Mertens)
#### Post date: [January 9, 2020, 10:38am UTC](https://discuss.elastic.co/t/aggregate-doc-count-per-document-and-not-per-nested-item/214416/1 "2020-01-09T10:38:15Z")

</div>

Hi, my documents have the following document structure (I changed the document type to frog and colors per year here to make it more clear):

```
{
    "id": "489191",
    "type": "frog",
    "color_types": [
        {
            "year": "1990",
            "colors": [
                "red",
                "green"
            ]
        },
        {
            "year": "1991",
            "colors": [
                "red",
                "blue"
            ]
        }
    ]
}

```

Now if I do an aggregation on this as folows, I get 2 for doc\_count on red.

```
{
    "aggs": {
        "color_aggregation": {
            "nested": {
                "path": "color_types"
            },
            "aggs": {
                "nested": {
                    "terms": {
                        "field": "color_types.colors",
                            "order": {
                            "_term": "desc"
                        },
                        "size": 10
                    }
                }
            }
        }
    }
}

```

These are the results:

```
{
    "key": "red",
    "doc_count": 2
},
{
    "key": "green",
    "doc_count": 1
},
{
    "key": "blue",
    "doc_count": 1
},

```

As you can see I get 2 for red, whereas there is only one frog. I know this is normal behaviour, but how can I change the doc\_count to count the number of documents (frogs) and not the number of nested items?

Thanks in advance for your help.

---

<div class="post-metadata">

### Author: ![Laurens\_Mertens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laurens_mertens/32/52225_2.png) [@Laurens\_Mertens](https://discuss.elastic.co/u/Laurens_Mertens)
#### Post date: [January 9, 2020, 10:56am UTC](https://discuss.elastic.co/t/aggregate-doc-count-per-document-and-not-per-nested-item/214416/2 "2020-01-09T10:56:37Z")

</div>

OK it seems like this works

```
{
    "aggs": {
        "color_aggregation": {
            "nested": {
                "path": "color_types"
            },
            "aggs": {
                "nested": {
                    "terms": {
                        "field": "color_types.colors",
                            "order": {
                            "_term": "desc"
                        },
                        "size": 10
                    },
                     "aggs": {
                         "reversed_nested": {
                             "reverse_nested": {
                         }
                      }
                    }
                }
            }
        }
    }
}
```

---

<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: [February 6, 2020, 10:56am UTC](https://discuss.elastic.co/t/aggregate-doc-count-per-document-and-not-per-nested-item/214416/3 "2020-02-06T10:56:38Z")

</div>

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