# Agg count only once per parent in nested objects

**URL:** <https://discuss.elastic.co/t/agg-count-only-once-per-parent-in-nested-objects/217267>\
**Category:** Elasticsearch\
**Created:** [January 30, 2020, 7:57pm UTC](https://discuss.elastic.co/t/agg-count-only-once-per-parent-in-nested-objects/217267 "2020-01-30T19:57:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philipp\_Loew](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philipp_loew/32/48045_2.png) [@Philipp\_Loew](https://discuss.elastic.co/u/Philipp_Loew)\
**Post date:** [January 30, 2020, 7:57pm UTC](https://discuss.elastic.co/t/agg-count-only-once-per-parent-in-nested-objects/217267/1 "2020-01-30T19:57:34Z")

</div>

Hi there,

I have a mapping with nested objects, that we use to combine variants to a main product. we do nested aggregations on the variants to find values of certain keys.

The agg counts are incremented by one, for each variant that hits the value.  
I would like to get only one count for the agg for each main product.

here you find an example of our mapping:

```auto
    {
"products": {
    "mappings": {
        "dynamic": "false",
        "properties": {
            "product_id": {
                "type": "keyword"
            },
            "variants": {
                "type": "nested",
                "properties": {
                    "brand": {
                        "type": "keyword"
                    },
                    "color": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

```

this is an example of our product-structure:

```
{
        "_index": "products",
        "_type": "_doc",
        "_id": "5e42f759de235d5b42e6e35524141bd2cfcbc5d0",
        "_score": 1.0,
        "_source": {
            "product_id": "5e42f759de235d5b42e6e35524141bd2cfcbc5d0",
            "variants": [
                {

                    "color": [
                        "Black"
                    ]
                    "brand": "Fackelmann"
                },
                {
                    "color": [],
                    "brand": "Fackelmann"
                },
                {
                    "color": [],
                    "brand": "Fackelmann",
                },
                {
                    "color": [],
                    "brand": "Fackelmann"
                }
            ]
        }
    },
    {
        "_index": "products",
        "_type": "_doc",
        "_id": "768af68018654b04f20d32003348ee9bd81d9f65",
        "_score": 1.0,
        "_source": {
            "product_id": "768af68018654b04f20d32003348ee9bd81d9f65",
            "variants": [
                {

                    "color": [
                        "Grey"
                    ]
                    "brand": "Fackelmann"
                },
                {
                    "color": [],
                    "brand": "Fackelmann"
                },
                {
                    "color": [],
                    "brand": "Fackelmann",
                },
                {
                    "color": [],
                    "brand": "IKEA"
                }
            ]
        }
    }

```

in this example we get those counts for aggs:

```auto
brand:Fackelmann[7]
brand:IKEA[1]
color:Grey[1]
color:Black[1]

```

What i expect to get is:

```auto
brand:Fackelmann[2]
brand:IKEA[1]
color:Grey[1]
color:Black[1]

```

we already tried reverse nested aggregations but without luck ☹

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 31, 2020, 9:45am UTC](https://discuss.elastic.co/t/agg-count-only-once-per-parent-in-nested-objects/217267/2 "2020-01-31T09:45:53Z")

</div>

How about storing the distinct set of brands in an own keyword field outside of the `variants` array like `"brands" : ["Fackelmann", "IKEA"]` and then running a terms aggregation on that one?

You could add an indexing pipeline to do that.

---

<div class="post-metadata">

**Author:** ![mkhludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkhludnev/32/97857_2.png) [@mkhludnev](https://discuss.elastic.co/u/mkhludnev)\
**Post date:** [February 2, 2020, 3:37pm UTC](https://discuss.elastic.co/t/agg-count-only-once-per-parent-in-nested-objects/217267/3 "2020-02-02T15:37:22Z")

</div>

I suppose, reverse nested aggregation is the only an option.

---

<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:** [March 1, 2020, 3:37pm UTC](https://discuss.elastic.co/t/agg-count-only-once-per-parent-in-nested-objects/217267/4 "2020-03-01T15:37:29Z")

</div>

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