# \[Elasticsearch 5.4.x\] Return additional data when using aggregations?

**URL:** <https://discuss.elastic.co/t/elasticsearch-5-4-x-return-additional-data-when-using-aggregations/113243>\
**Category:** Elasticsearch\
**Created:** [December 26, 2017, 7:14pm UTC](https://discuss.elastic.co/t/elasticsearch-5-4-x-return-additional-data-when-using-aggregations/113243 "2017-12-26T19:14:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![west415](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@west415](https://discuss.elastic.co/u/west415)\
**Post date:** [December 26, 2017, 7:14pm UTC](https://discuss.elastic.co/t/elasticsearch-5-4-x-return-additional-data-when-using-aggregations/113243/1 "2017-12-26T19:14:04Z")

</div>

Hi,

I am currently using the bucket aggregation functionality in Elastic and it's working fine, but wanted to know if there is a way to return additional data dynamically as part of the aggregation results.

I have this json

```
"from": 0,
"size": 9,
"aggs": {
   "group_by_work_location": {
       "terms": {
          "field": "work_location.keyword"
      }
   }
}

```

It returns a response like:

```
  "aggregations": {
     "group_by_work_location": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "Chicago, IL",
                "doc_count": 2
            }
        ]
    }
}

```

Based on the above, is there a way to return an additional field/key **dynamically** within the bucket array? I have a field in the data called "location\_id". I would like to return this field alongside the "key" and "doc\_count". Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 27, 2017, 12:26pm UTC](https://discuss.elastic.co/t/elasticsearch-5-4-x-return-additional-data-when-using-aggregations/113243/2 "2017-12-27T12:26:57Z")

</div>

Hi @west415,

I think a sub-aggregation can help you:

```
POST location/doc/1
{
  "work_location": "Chicago",
  "location_id": 123
}

POST location/doc/2
{
  "work_location": "New York",
  "location_id": 321
}

GET location/_search
{
  "size": 0,
  "aggs": {
    "group_by_work_location": {
      "terms": {
        "field": "work_location.keyword",
        "size": 10
      },
      "aggs": {
        "location_id": {
          "terms": {
            "field": "location_id",
            "size": 10
          }
        }
      }
    }
  }
} 

```

Result:

```
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_work_location": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Chicago",
          "doc_count": 1,
          "location_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 123,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "New York",
          "doc_count": 1,
          "location_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 321,
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

```

Best,  
LG

---

<div class="post-metadata">

**Author:** ![west415](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@west415](https://discuss.elastic.co/u/west415)\
**Post date:** [December 29, 2017, 9:48pm UTC](https://discuss.elastic.co/t/elasticsearch-5-4-x-return-additional-data-when-using-aggregations/113243/3 "2017-12-29T21:48:12Z")

</div>

Thanks, @luiz.santos, I just tried it and it works fine. Thanks for the tip.

---

<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:** [January 26, 2018, 9:48pm UTC](https://discuss.elastic.co/t/elasticsearch-5-4-x-return-additional-data-when-using-aggregations/113243/4 "2018-01-26T21:48:16Z")

</div>

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