# Return a different field than the aggregated field

**URL:** <https://discuss.elastic.co/t/return-a-different-field-than-the-aggregated-field/28271>\
**Category:** Elasticsearch\
**Created:** [August 28, 2015, 10:35pm UTC](https://discuss.elastic.co/t/return-a-different-field-than-the-aggregated-field/28271 "2015-08-28T22:35:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![clofresh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clofresh/32/3680_2.png) [@clofresh](https://discuss.elastic.co/u/clofresh)\
**Post date:** [August 28, 2015, 10:35pm UTC](https://discuss.elastic.co/t/return-a-different-field-than-the-aggregated-field/28271/1 "2015-08-28T22:35:55Z")

</div>

Hi,

Given documents that look like this:

```
[
  {
    id: 10,
    aggregation_key: "foo",
    timestamp: "2015-08-28"
  },
  {
    id: 20,
    aggregation_key: "foo",
    timestamp: "2015-08-27"
  },
  {
    id: 30,
    aggregation_key: "bar",
    timestamp: "2015-08-25"
  },
]

```

Can someone tell me if this is possible with the aggregation api:

I want to group documents together by aggregation\_key, and for each set of documents with the same aggregation\_key, output the id of the latest document.

So for the above example, it'd return something like:

```
[
  {
    "foo": 10,
    "bar": 30
  }
]

```

.Carlo

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 28, 2015, 10:59pm UTC](https://discuss.elastic.co/t/return-a-different-field-than-the-aggregated-field/28271/2 "2015-08-28T22:59:57Z")

</div>

You can do that with a [`top_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) embedded inside of a `terms`:

```json
{
    "aggs": {
        "keys": {
            "terms": {
                "field": "aggregation_key"
            },
            "aggs": {
                "top_id": {
                    "top_hits": {
                        "sort": [
                            { "id": { "order": "desc" } }
                        ],
                        "_source": {
                            "include": ["id"]
                        },
                        "size" : 1
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![clofresh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clofresh/32/3680_2.png) [@clofresh](https://discuss.elastic.co/u/clofresh)\
**Post date:** [August 29, 2015, 1:59pm UTC](https://discuss.elastic.co/t/return-a-different-field-than-the-aggregated-field/28271/3 "2015-08-29T13:59:08Z")

</div>

That's exactly what I was looking for, thanks!

---

<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:** [July 5, 2017, 11:53pm UTC](https://discuss.elastic.co/t/return-a-different-field-than-the-aggregated-field/28271/4 "2017-07-05T23:53:11Z")

</div>


