# Add different fields to aggregations results

**URL:** <https://discuss.elastic.co/t/add-different-fields-to-aggregations-results/28649>\
**Category:** Elasticsearch\
**Created:** [September 4, 2015, 3:33am UTC](https://discuss.elastic.co/t/add-different-fields-to-aggregations-results/28649 "2015-09-04T03:33:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen\_Shilavantar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/praveen_shilavantar/32/1619_2.png) [@Praveen\_Shilavantar](https://discuss.elastic.co/u/Praveen_Shilavantar)\
**Post date:** [September 4, 2015, 3:33am UTC](https://discuss.elastic.co/t/add-different-fields-to-aggregations-results/28649/1 "2015-09-04T03:33:15Z")

</div>

Sample Index data:

![](https://us1.discourse-cdn.com/elastic/original/2X/b/b45edbd193c2f7d1cf70bf25268fc12675411aed.png)

Desired output:

![](https://us1.discourse-cdn.com/elastic/original/2X/1/151d3413ee5dbec1529964d58125d33506a7138e.png)

SQL Query which can yield the above output:

SELECT name, component\_version  
FROM config\_data  
WHERE report\_date BETWEEN '2015-08-26' AND '2015-08-27'  
GROUP BY name  
ORDER BY report\_date DESC;

I don't know how to add "component\_version" to aggregations results? I am not sure it's possible.

{  
"size": 2,  
"\_source": {  
"includes": [  
"tail",  
"report\_date",  
"component\_version"  
],  
"excludes":   
},  
"sort": [  
{  
"report\_date": {  
"order": "desc"  
}  
}  
],  
"aggs": {  
"tail": {  
"terms": {  
"field": "tail"  
}  
}  
}  
}

I am looking for the below output,

"aggregations": {  
"name": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 1130419,  
"buckets": [  
{  
**"key": "ABC",**  
**"report\_date": "2015-08-26T01:17:07"**  
**"component\_version": "1.03"**  
},  
{  
**"key": "BCD",**  
**"report\_date": "2015-08-26T03:17:07"**  
**"component\_version": "3.03"**  
}  
}

---

<div class="post-metadata">

**Author:** ![Praveen\_Shilavantar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/praveen_shilavantar/32/1619_2.png) [@Praveen\_Shilavantar](https://discuss.elastic.co/u/Praveen_Shilavantar)\
**Post date:** [September 4, 2015, 3:46am UTC](https://discuss.elastic.co/t/add-different-fields-to-aggregations-results/28649/2 "2015-09-04T03:46:49Z")

</div>

I am able to achieve this using top hits aggregations

[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)

{  
"aggs": {  
"top-name": {  
"terms": {  
"field": "name",  
"size": 10  
},  
"aggs": {  
"top\_component\_versions": {  
"top\_hits": {  
"sort": [  
{  
"report\_date": {  
"order": "desc"  
}  
}  
],  
"\_source": {  
"include": [  
"name",  
"report\_date",  
"component\_version"  
]  
},  
"size" : 1  
}  
}  
}  
}  
}  
}

---

<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:52pm UTC](https://discuss.elastic.co/t/add-different-fields-to-aggregations-results/28649/3 "2017-07-05T23:52:08Z")

</div>


