# Return both aggregation AND field value in ES

**URL:** <https://discuss.elastic.co/t/return-both-aggregation-and-field-value-in-es/113985>\
**Category:** Elasticsearch\
**Created:** [January 3, 2018, 10:18pm UTC](https://discuss.elastic.co/t/return-both-aggregation-and-field-value-in-es/113985 "2018-01-03T22:18:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Natalia\_Kuleniuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/natalia_kuleniuk/32/43727_2.png) [@Natalia\_Kuleniuk](https://discuss.elastic.co/u/Natalia_Kuleniuk)\
**Post date:** [January 3, 2018, 10:18pm UTC](https://discuss.elastic.co/t/return-both-aggregation-and-field-value-in-es/113985/1 "2018-01-03T22:18:15Z")

</div>

Let’s say that each document in my ES index has following fields:

```
ASIN, name, referenceNumber, videoViews, timeWatched

```

There can be **many documents with the same ASIN** fields.

I’m doing teams aggregation, that aggregate documents in my ES cluster based on **ASIN field**.  
This Aggregation counts sum of **videoViews** and sum of **timeWatched**  **for each ASIN bucket**.

ElasticSearch returns aggregation response where **ASIN** - a key for each bucket, and sum of **timeWatched** and sum of **videoViews** are values for this key.

My simple question is how to make aggregation return **name** and **referenceNumber** as well? If I know that documents with same **ASIN** will have same **name** and **referenceNumber**?

---

<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:** [January 5, 2018, 2:11pm UTC](https://discuss.elastic.co/t/return-both-aggregation-and-field-value-in-es/113985/2 "2018-01-05T14:11:05Z")

</div>

If you know all documents with the same `ASIN` also have the same `name` and `referenceNumber` the easiest solution would be to use a `top_hits` agg inside the terms agg: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)

Set the size to `1`, which returns the "first" document. Since all ASIN/name/referenceNumber's correspond to each other, you don't have to worry about sorting. That'll give you the complete document, which you can grab any fields that you want from.

---

<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:** [February 2, 2018, 2:11pm UTC](https://discuss.elastic.co/t/return-both-aggregation-and-field-value-in-es/113985/3 "2018-02-02T14:11:07Z")

</div>

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