# ElasticSearch Aggregation Problem

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-problem/16485>\
**Category:** Elasticsearch\
**Created:** [March 20, 2014, 5:43am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-problem/16485 "2014-03-20T05:43:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumit\_gupta\_sgt](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@sumit\_gupta\_sgt](https://discuss.elastic.co/u/sumit_gupta_sgt)\
**Post date:** [March 20, 2014, 5:43am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-problem/16485/1 "2014-03-20T05:43:04Z")

</div>

HI All,

I have Document like that where CallTy and LegTy have values 0,1,2  
{  
\_index: klocal\_2014\_03\_20  
\_type: CDR  
\_id: zURwiQMYT02kBVH93lOzhg  
\_version: 1  
\_score: 1  
\_source: {  
type: CDR  
RecTy: 61  
FROM: 15867094430  
TO: 19316267860  
RecID: 660101  
CallTy: 0  
CallRef: 3119340  
LegTy: 1  
SeTime: 140131234920+0500  
AnsTim: 140131234920+0500  
ReTime: 140131234941+0500  
CallDur: 21  
TermRC: 0  
Diag: 16  
GID:  
PayTy: 0  
LOC: 310410  
MeIP: 199.47.227.30  
MPort: 38884  
SubTy: 2  
IMEI: 352702052011777  
PSTy: 0  
RemEn: 1  
@timestamp: 2014-03-20T10:02:44+05:30  
\_ttl: 2d  
}  
}

When i write Aggregation query for LegTy and SubTy it gives result like  
curl -XPOST "[http://localhost:9200/klocal\_2014\_03\_19/CDR/\_search?pretty](http://localhost:9200/klocal_2014_03_19/CDR/_search?pretty)" -d'  
{  
"size": 0,  
"aggregations": {  
"LegTy": {  
"terms": {  
"field": "LegTy"  
}  
},  
"SubTy": {  
"terms": {  
"field": "SubTy"  
}  
}  
}   
}'

Output:

{  
"took" : 13,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 241,  
"max\_score" : 0.0,  
"hits" : []  
},  
"aggregations" : {  
"LegTy" : {  
"buckets" : [ {  
"key" : 0,  
"doc\_count" : 120  
}, {  
"key" : 1,  
"doc\_count" : 119  
}, {  
"key" : 2,  
"doc\_count" : 2  
} ]  
},  
"SubTy" : {  
"buckets" : [ {  
"key" : 2,  
"doc\_count" : 225  
}, {  
"key" : 1,  
"doc\_count" : 16  
} ]  
}  
}  
}

But I want output Like

"key" : [0,2],  
"doc\_count" : XXX,  
"key" : [0,1],  
"doc\_count" : XXX,  
"key" : [1,2],  
"doc\_count" : XXX,  
"key" : [1,1],  
"doc\_count" : XXX,  
"key" : [2,2],  
"doc\_count" : XXX,  
"key" : [2,1],  
"doc\_count" : XXX

Please Suggest.

Thanks & Regards,  
Sumit Gupta

---

<div class="post-metadata">

**Author:** ![sumit\_gupta\_sgt](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@sumit\_gupta\_sgt](https://discuss.elastic.co/u/sumit_gupta_sgt)\
**Post date:** [March 20, 2014, 2:36pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-problem/16485/2 "2014-03-20T14:36:41Z")

</div>

Solve it using Aggregating Hierarchical Data.

Regards,  
Sumit Gupta

---

<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, 1:41am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-problem/16485/3 "2017-07-06T01:41:50Z")

</div>


