# How to add a field to the result of the aggregation

**URL:** <https://discuss.elastic.co/t/how-to-add-a-field-to-the-result-of-the-aggregation/279581>\
**Category:** Elasticsearch\
**Created:** [July 26, 2021, 2:23am UTC](https://discuss.elastic.co/t/how-to-add-a-field-to-the-result-of-the-aggregation/279581 "2021-07-26T02:23:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wajika](https://avatars.discourse-cdn.com/v4/letter/w/977dab/32.png) [@wajika](https://discuss.elastic.co/u/wajika)\
**Post date:** [July 26, 2021, 2:23am UTC](https://discuss.elastic.co/t/how-to-add-a-field-to-the-result-of-the-aggregation/279581/1 "2021-07-26T02:23:28Z")

</div>

![微信截图_20210726101715](https://us1.discourse-cdn.com/elastic/original/3X/0/6/0618d090b830de99e27aa16c95b578c87683d54f.png)  
I wrote an aggregation, and it meets my requirements, but I also need to add a field (trace.id) when the results are displayed. Trace.id does not need to be aggregated.  
How should dsl be written in this case?  
Can anyone help

```auto
POST apm-7.8.1-transaction-000012/_search
{
  "size": 0,
  "aggs": {
    "range": {
      "date_range": {
        "field": "date",
        "format": "MM-yyyy",
        "ranges": [
          {
            "to": "now-10M/M"
          },
          {
            "from": "now-10M/M"
          }
        ]
      }
    },
    "service_name": {
      "terms": {
        "field": "service.name"
      },
      "aggs": {
        "url_path": {
          "terms": {
            "field": "url.path",
            "order": [
              {
                "transaction_duration_us": "desc"
              }
            ],
            "size": 10
          },
          "aggs": {
            "transaction_duration_us": {
              "avg": {
                "field": "transaction.duration.us"
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [July 26, 2021, 7:29am UTC](https://discuss.elastic.co/t/how-to-add-a-field-to-the-result-of-the-aggregation/279581/2 "2021-07-26T07:29:39Z")

</div>

Do you expect this field to be the same for all the documents in a bucket? If not, which value should be displayed? I am a little unsure of the requirements, thus asking again...

What is the information you are trying to get?

---

<div class="post-metadata">

**Author:** ![wajika](https://avatars.discourse-cdn.com/v4/letter/w/977dab/32.png) [@wajika](https://discuss.elastic.co/u/wajika)\
**Post date:** [July 26, 2021, 8:24am UTC](https://discuss.elastic.co/t/how-to-add-a-field-to-the-result-of-the-aggregation/279581/3 "2021-07-26T08:24:26Z")

</div>

I want to get some data from the apm error index. The data needs to be sorted (implemented by elasticsearch for the time being). Because the fields displayed by the aggregation operation cannot meet my requirements, I want to add another field, but this field does not need to be added to the aggregation .

I have a python code that will generate an excel sheet from the aggregated result.

 ![微信截图_20210726162357](https://us1.discourse-cdn.com/elastic/original/3X/d/a/da85a28aa653ea76165eb41b09c2643695e3dc97.png)

---

<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:** [August 23, 2021, 8:24am UTC](https://discuss.elastic.co/t/how-to-add-a-field-to-the-result-of-the-aggregation/279581/4 "2021-08-23T08:24:44Z")

</div>

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