# Terms aggregation on an inner object and retrieving bucket metadata

**URL:** <https://discuss.elastic.co/t/terms-aggregation-on-an-inner-object-and-retrieving-bucket-metadata/223337>\
**Category:** Elasticsearch\
**Created:** [March 12, 2020, 1:20pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-an-inner-object-and-retrieving-bucket-metadata/223337 "2020-03-12T13:20:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre\_n](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@pierre\_n](https://discuss.elastic.co/u/pierre_n)\
**Post date:** [March 12, 2020, 1:20pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-an-inner-object-and-retrieving-bucket-metadata/223337/1 "2020-03-12T13:20:28Z")

</div>

We index the following products:

```auto
{
    "id": "1",
    "name": "the-name",
    "categories": [
        {
            "id" : 10,
            "name" : "cat-1"
        },
        {
            "id" : 20,
            "name" : "cat-2"
        }
    ]
}

```

We are doing an aggregation on `categories.id` using :

```auto
REQUEST:
//...
"aggs": {
    "by_cat": {
        "terms": {
            "field": "categories.id",
            "size": 10
        }
    }
}

---
RESPONSE:
// ...
"by_cat" : {
    "buckets" : [
    {
        "key" : 10,
        "doc_count" : 804
    },
    {
        "key" : 20,
        "doc_count" : 327
    },

```

It works well, however, each bucket contains only the `categories.id` in the `key` field. What we would like is to be able to have the name of the category in the bucket, for example :

```auto
// ...
      "buckets" : [
        {
          "key" : 10,
          "metadata": {
              "name": "cat-1"
          },
          "doc_count" : 804
        },
        {
          "key" : 20,
          "metadata": {
              "name": "cat-1"
          },
          "doc_count" : 327
        },

```

What is the good way to do that ? We found two to get this information but they both looks "hackish" :

- Using top\_hits with size 1 and source limited to categories, it will retrieve one document per bucket containing the information we need. This first solution doesn't look performance-wise and the more aggregation we have, the more bloated is the response.
- Adding a new column id\_name which concatenate id and name and doing the term aggregation on it. It looks more like a hack, and may be complicated if many fields.

We also tried by mixing field and script in terms but it doesn't help.

[metadata](https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html) looked exactly what we wanted but it is global for all the buckets and not dynamic.

Do we have other way to retrieve this information ?

---

<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:** [April 9, 2020, 1:20pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-an-inner-object-and-retrieving-bucket-metadata/223337/2 "2020-04-09T13:20:35Z")

</div>

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