# How to display other \_source fields in a metric aggregation result?

**URL:** <https://discuss.elastic.co/t/how-to-display-other-source-fields-in-a-metric-aggregation-result/280209>\
**Category:** Elasticsearch\
**Created:** [August 2, 2021, 10:53am UTC](https://discuss.elastic.co/t/how-to-display-other-source-fields-in-a-metric-aggregation-result/280209 "2021-08-02T10:53:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![StephaneB](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@StephaneB](https://discuss.elastic.co/u/StephaneB)\
**Post date:** [August 2, 2021, 10:53am UTC](https://discuss.elastic.co/t/how-to-display-other-source-fields-in-a-metric-aggregation-result/280209/1 "2021-08-02T10:53:16Z")

</div>

Hello,

I'm working on ML job documents with the following fields:

```auto
      {
        "_index" : ".ml-anomalies-custom-job_device1",
        "_type" : "_doc",
        "_id" : "job_device1_model_forecast_Gwn4wnoB0F2uUBpmgO6V_1626768000000_900_0_0_0",
        "_score" : 0.0,
        "_source" : {
          "job_id" : "job_device1",
          "forecast_id" : "Gwn4wnoB0F2uUBpmgO6V",
          "result_type" : "model_forecast",
          "bucket_span" : 900,
          "detector_index" : 0,
          "timestamp" : 1626768000000,
          "model_feature" : "'arithmetic mean value by person'",
          "forecast_lower" : 72.46782289572343,
          "forecast_upper" : 73.54060395699805,
          "forecast_prediction" : 73.00421342636075
        }

```

I create the following min aggregation in order to find the minimal value of the `forecast_prediction` in all the `model_forecast` ML job documents :

```auto
GET .ml-anomalies*/_search
{
  "size": 1,
  "query": {
    "bool" :{
      "filter": [
        { 
          "query_string":{
            "query": "result_type:model_forecast"
          }
        },
        {"term": { "job_id": "job_device1"}}]
    }
  },
"aggs": {
  "forecast_min": { "min": { "field": "forecast_prediction" } }
  }
}

```

It leads to the following result:

```auto
{
"aggregations" : {
    "forecast_min" : {
      "value" : 72.99105325040645
    }
  }
}

```

But I woulk like to be able:

- to display other fields in order to know what is for example the `timestamp` associated to the minimal value of this `forecast_prediction`
- to know what is the `_id` for the document in which the `forecast_prediction` is the minimum

Is there any way to display something like:

```auto
{
"aggregations" : {
    "forecast_min" : {
      "value" : 72.99105325040645,
      "timestamp" : 1626767100000,
      "other_fields_of_the_same_document" : 12345678
      ...
    }
  }
}

```

---

<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:** [August 2, 2021, 11:52am UTC](https://discuss.elastic.co/t/how-to-display-other-source-fields-in-a-metric-aggregation-result/280209/2 "2021-08-02T11:52:56Z")

</div>

I am not sure you need an aggregation for this? How about sorting by the `forecast_prediction ` field and only returning a single document?

---

<div class="post-metadata">

**Author:** ![StephaneB](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@StephaneB](https://discuss.elastic.co/u/StephaneB)\
**Post date:** [August 2, 2021, 12:43pm UTC](https://discuss.elastic.co/t/how-to-display-other-source-fields-in-a-metric-aggregation-result/280209/3 "2021-08-02T12:43:06Z")

</div>

Thanks @spinscale.

Effectively it does the job very easily! I was so obsessed with aggregations....

---

<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 30, 2021, 12:43pm UTC](https://discuss.elastic.co/t/how-to-display-other-source-fields-in-a-metric-aggregation-result/280209/4 "2021-08-30T12:43:31Z")

</div>

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