# Aggregation buckets, with an additional key:value inside

**URL:** <https://discuss.elastic.co/t/aggregation-buckets-with-an-additional-key-value-inside/20427>\
**Category:** Elasticsearch\
**Created:** [October 24, 2014, 9:50pm UTC](https://discuss.elastic.co/t/aggregation-buckets-with-an-additional-key-value-inside/20427 "2014-10-24T21:50:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cody\_Stringham](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cody_stringham/32/944_2.png) [@Cody\_Stringham](https://discuss.elastic.co/u/Cody_Stringham)\
**Post date:** [October 24, 2014, 9:50pm UTC](https://discuss.elastic.co/t/aggregation-buckets-with-an-additional-key-value-inside/20427/1 "2014-10-24T21:50:27Z")

</div>

Hey everyone,

These aggregations are working out great, but I need to return more than  
one value in the bucket so we can use them in our API. The basic idea is  
that we aggregate all of the category id's, but we also want the  
category\_name to be included in that same bucket for ease of use.

_Mapping:_  
"categories" : {  
"properties" : {  
"category\_name" : {  
"analyzer" : "keyword",  
"type" : "string"  
},  
"category\_id" : {  
"type" : "integer"  
},  
"parent\_id" : {  
"type" : "integer"  
}  
}  
}

_Aggs:_  
aggs: {  
categories: {  
terms: {  
size: 130,  
field: "categories.category\_id"  
}  
},

_Returns (actual):_

"category\_stats": [  
{  
"category\_id": 58,  
"offer\_count": 48885  
},  
{  
"category\_id": 1008,  
"offer\_count": 44530  
},

```
...

```

_Returns (desired):_

"category\_stats": [  
{  
"category\_name": "Car Rental",  
"category\_id": 58,  
"offer\_count": 48885  
},  
{  
"category\_name": "Fast Food",  
"category\_id": 1008,  
"offer\_count": 44530  
},

```
...

```

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/055d0c2b-f4ba-455b-883f-587c09b61582%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/055d0c2b-f4ba-455b-883f-587c09b61582%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 25, 2014, 4:03pm UTC](https://discuss.elastic.co/t/aggregation-buckets-with-an-additional-key-value-inside/20427/2 "2014-10-25T16:03:55Z")

</div>

I maintain a mapping on the client side to due the lookups. Thankfully my  
taxonomy is static (but somewhat large). There is a PR to do server-side  
mappings, but I don't think it would apply to aggregations and is quite old.

An alternative solution would be to create compound values such as  
"48885:Car Rental" and decompose the value on the client side, but this  
would create a string aggregation, which could have slower performance.

Cheers,

Ivan

On Fri, Oct 24, 2014 at 5:50 PM, Cody Stringham [cs.negativ@gmail.com](mailto:cs.negativ@gmail.com)  
wrote:

> Hey everyone,
> 
> These aggregations are working out great, but I need to return more than  
> one value in the bucket so we can use them in our API. The basic idea is  
> that we aggregate all of the category id's, but we also want the  
> category\_name to be included in that same bucket for ease of use.
> 
> _Mapping:_  
> "categories" : {  
> "properties" : {  
> "category\_name" : {  
> "analyzer" : "keyword",  
> "type" : "string"  
> },  
> "category\_id" : {  
> "type" : "integer"  
> },  
> "parent\_id" : {  
> "type" : "integer"  
> }  
> }  
> }
> 
> _Aggs:_  
> aggs: {  
> categories: {  
> terms: {  
> size: 130,  
> field: "categories.category\_id"  
> }  
> },
> 
> _Returns (actual):_
> 
> "category\_stats": [  
> {  
> "category\_id": 58,  
> "offer\_count": 48885  
> },  
> {  
> "category\_id": 1008,  
> "offer\_count": 44530  
> },
> 
> ```
> ...
> 
> ```
> 
> _Returns (desired):_
> 
> "category\_stats": [  
> {  
> "category\_name": "Car Rental",  
> "category\_id": 58,  
> "offer\_count": 48885  
> },  
> {  
> "category\_name": "Fast Food",  
> "category\_id": 1008,  
> "offer\_count": 44530  
> },
> 
> ```
> ...
> 
> ```
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/055d0c2b-f4ba-455b-883f-587c09b61582%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/055d0c2b-f4ba-455b-883f-587c09b61582%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/055d0c2b-f4ba-455b-883f-587c09b61582%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/055d0c2b-f4ba-455b-883f-587c09b61582%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQB-Asuq5igGP-mJQ7RGv4t2CjsjryBGSTPDn0EAb-vfZw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQB-Asuq5igGP-mJQ7RGv4t2CjsjryBGSTPDn0EAb-vfZw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:53am UTC](https://discuss.elastic.co/t/aggregation-buckets-with-an-additional-key-value-inside/20427/3 "2017-07-06T00:53:53Z")

</div>


