# Nested Composite Aggregations with ElasticSearch

**URL:** <https://discuss.elastic.co/t/nested-composite-aggregations-with-elasticsearch/295282>\
**Category:** Elasticsearch\
**Created:** [January 24, 2022, 8:35pm UTC](https://discuss.elastic.co/t/nested-composite-aggregations-with-elasticsearch/295282 "2022-01-24T20:35:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [January 24, 2022, 8:35pm UTC](https://discuss.elastic.co/t/nested-composite-aggregations-with-elasticsearch/295282/1 "2022-01-24T20:35:17Z")

</div>

I have an index that stores sales by salesperson, and each sale has a service rating. Using `composite` I get these separate ratings within an object by duplicating the seller ID. Is it possible to nest within a single object per seller the count per review?

This is the result I need, organized and easy to locate data:

```auto
{
    [
        {
            "operator": 79852,
            "doc_count": 1,
            "reviews": {
                "review": "Good",
                "doc_count": 1
            }
        },
        {
            "operator": 79855,
            "doc_count": 3,
            "reviews": {
                [
                    {
                        "review": "Good",
                        "doc_count": 2
                    },
                    {
                        "review": "Bad",
                        "doc_count": 1
                    }
                ]
            }
        }
    ]
}

```

My code currently looks like this:

```auto
{
    ...
    "aggregations": {
        "groupby": {
            "composite": {
                "size": 1000,
                "sources": [
                    {
                        "operator": {
                            "terms": {
                                "field": "operatorId",
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    },
                    {
                        "review": {
                            "terms": {
                                "field": "review",
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    }
                ]
            }
        }
    }
}

```

Result:

```auto
{
    ...
    "hits": {
        "total": 5,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "groupby": {
            "after_key": {
                "operator": 79855,
                "review": "Bad"
            },
            "buckets": [
                {
                    "key": {
                        "operator": 79852,
                        "review": "Bad"
                    },
                    "doc_count": 2
                },
                {
                    "key": {
                        "operator": 79852,
                        "review": "Good"
                    },
                    "doc_count": 1
                },
                {
                    "key": {
                        "operator": 79852,
                        "review": "Regular"
                    },
                    "doc_count": 1
                },
                {
                    "key": {
                        "operator": 79855,
                        "review": "Bad"
                    },
                    "doc_count": 1
                }
            ]
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 25, 2022, 12:16am UTC](https://discuss.elastic.co/t/nested-composite-aggregations-with-elasticsearch/295282/2 "2022-01-25T00:16:37Z")

</div>

Hi, try sub aggregation.

> **[Intro to Aggregations pt. 2: Sub-Aggregations](https://www.elastic.co/jp/blog/intro-to-aggregations-pt-2-sub-aggregations)**

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [January 25, 2022, 1:48pm UTC](https://discuss.elastic.co/t/nested-composite-aggregations-with-elasticsearch/295282/3 "2022-01-25T13:48:54Z")

</div>

Thanks.

Two Solutions

```auto
{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggs": {
        "datagroup": {
            "terms": {
                "field": "sellerid"
            },
            "aggs": {
                "good": {
                    "sum": {
                        "script": "(doc['review'].value=='G')?1:0"
                    }
                },
                "bad": {
                    "sum": {
                        "script": "(doc['review'].value=='B')?1:0"
                    }
                },
                "regular": {
                    "sum": {
                        "script": "(doc['review'].value=='N')?1:0"
                    }
                }
            }
        }
    }
}

```

```auto
{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggs": {
        "datagroup": {
            "terms": {
                "field": "sellerid"
            },
            "aggs": {
                "review": {
                    "terms": {
                        "field": "review"
                    }
                }
            }
        }
    }
}

```

---

<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 22, 2022, 1:49pm UTC](https://discuss.elastic.co/t/nested-composite-aggregations-with-elasticsearch/295282/4 "2022-02-22T13:49:28Z")

</div>

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