# Joining Multiple Aggregration Queries into one query

**URL:** https://discuss.elastic.co/t/joining-multiple-aggregration-queries-into-one-query/22673
**Category:** Elasticsearch
**Created:** [March 15, 2015, 6:02pm UTC](https://discuss.elastic.co/t/joining-multiple-aggregration-queries-into-one-query/22673 "2015-03-15T18:02:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![prashant5375](https://avatars.discourse-cdn.com/v4/letter/p/4491bb/32.png) [@prashant5375](https://discuss.elastic.co/u/prashant5375)
#### Post date: [March 15, 2015, 6:02pm UTC](https://discuss.elastic.co/t/joining-multiple-aggregration-queries-into-one-query/22673/1 "2015-03-15T18:02:19Z")

</div>

## Hi , I have One query given below .

{  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "\*"  
}  
},  
"filter": {  
"bool": {  
"should": [  
{  
"fquery": {  
"query": {  
"query\_string": {  
"query": ""357539019562335""  
}  
},  
"\_cache": true  
}  
},  
{  
"fquery": {  
"query": {  
"query\_string": {  
"query": ""357539019562624""  
}  
},  
"\_cache": true  
}  
}  
],  
"must": [  
{  
"fquery": {  
"query": {  
"query\_string": {  
"query": "SMSATTEMPTSMO + SMSATTEMPTSMT"  
}  
},  
"\_cache": true  
}  
},  
{  
"range": {  
"@timestamp": {  
"to": "2015-03-05T00:00-05:00",  
"from": "2015-03-04T00:00-05:00"  
}  
}  
}  
]  
}  
}  
}  
},  
"aggs": {  
"events\_by\_IMEI": {  
"terms": {  
"field": "IMEI"  
},  
"aggs": {  
"sms\_over\_time": {  
"date\_histogram": {  
"field": "@timestamp",  
"interval": "1h",  
"post\_zone": "-05:00",  
"min\_doc\_count": 0,  
"extended\_bounds": {  
"min": "2015-03-04T00:00-05:00",  
"max": "2015-03-05T00:00-05:00"  
}  
},  
"aggs": {  
"total\_sms": {  
"sum": {  
"field": "value"  
}  
}  
}  
}  
}  
}  
},  
"size": 0  
}  
Which gives me below response.

* * *

## { "took": 9279, "timed\_out": false, "\_shards": { "total": 20, "successful": 20, "failed": 0 }, "hits": { "total": 354, "max\_score": 0, "hits": [] }, "aggregations": { "events\_by\_IMEI": { "buckets": [{ "key": "357539019562624", "doc\_count": 178, "sms\_over\_time": { "buckets": [ { "key\_as\_string": "2015-03-04T00:00:00.000Z", "key": 1425427200000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T01:00:00.000Z", "key": 1425430800000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T02:00:00.000Z", "key": 1425434400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T03:00:00.000Z", "key": 1425438000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T04:00:00.000Z", "key": 1425441600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T05:00:00.000Z", "key": 1425445200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T06:00:00.000Z", "key": 1425448800000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T07:00:00.000Z", "key": 1425452400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T08:00:00.000Z", "key": 1425456000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T09:00:00.000Z", "key": 1425459600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T10:00:00.000Z", "key": 1425463200000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T11:00:00.000Z", "key": 1425466800000, "doc\_count": 8, "total\_sms": { "value": 8 } }, { "key\_as\_string": "2015-03-04T12:00:00.000Z", "key": 1425470400000, "doc\_count": 8, "total\_sms": { "value": 3 } }, { "key\_as\_string": "2015-03-04T13:00:00.000Z", "key": 1425474000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T14:00:00.000Z", "key": 1425477600000, "doc\_count": 8, "total\_sms": { "value": 2 } }, { "key\_as\_string": "2015-03-04T15:00:00.000Z", "key": 1425481200000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T16:00:00.000Z", "key": 1425484800000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T17:00:00.000Z", "key": 1425488400000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T18:00:00.000Z", "key": 1425492000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T19:00:00.000Z", "key": 1425495600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T20:00:00.000Z", "key": 1425499200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T21:00:00.000Z", "key": 1425502800000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T22:00:00.000Z", "key": 1425506400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T23:00:00.000Z", "key": 1425510000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-05T00:00:00.000Z", "key": 1425513600000, "doc\_count": 2, "total\_sms": { "value": 0 } }] } }, { "key": "357539019562335", "doc\_count": 176, "sms\_over\_time": { "buckets": [{ "key\_as\_string": "2015-03-04T00:00:00.000Z", "key": 1425427200000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T01:00:00.000Z", "key": 1425430800000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T02:00:00.000Z", "key": 1425434400000, "doc\_count": 6, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T03:00:00.000Z", "key": 1425438000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T04:00:00.000Z", "key": 1425441600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T05:00:00.000Z", "key": 1425445200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T06:00:00.000Z", "key": 1425448800000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T07:00:00.000Z", "key": 1425452400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T08:00:00.000Z", "key": 1425456000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T09:00:00.000Z", "key": 1425459600000, "doc\_count": 8, "total\_sms": { "value": 3 } }, { "key\_as\_string": "2015-03-04T10:00:00.000Z", "key": 1425463200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T11:00:00.000Z", "key": 1425466800000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T12:00:00.000Z", "key": 1425470400000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T13:00:00.000Z", "key": 1425474000000, "doc\_count": 8, "total\_sms": { "value": 3 } }, { "key\_as\_string": "2015-03-04T14:00:00.000Z", "key": 1425477600000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T15:00:00.000Z", "key": 1425481200000, "doc\_count": 8, "total\_sms": { "value": 10 } }, { "key\_as\_string": "2015-03-04T16:00:00.000Z", "key": 1425484800000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T17:00:00.000Z", "key": 1425488400000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T18:00:00.000Z", "key": 1425492000000, "doc\_count": 8, "total\_sms": { "value": 2 } }, { "key\_as\_string": "2015-03-04T19:00:00.000Z", "key": 1425495600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T20:00:00.000Z", "key": 1425499200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T21:00:00.000Z", "key": 1425502800000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T22:00:00.000Z", "key": 1425506400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T23:00:00.000Z", "key": 1425510000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-05T00:00:00.000Z", "key": 1425513600000, "doc\_count": 2, "total\_sms": { "value": 0 } }] } } ] } } }

So my question here is if i have to combine 2 or more such queries and expect the response in different-2 buckets of buckets , how  
would we do that.

--  
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/c46ab7b0-8f96-473a-b97f-99a9c9b6c370%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c46ab7b0-8f96-473a-b97f-99a9c9b6c370%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![prashant5375](https://avatars.discourse-cdn.com/v4/letter/p/4491bb/32.png) [@prashant5375](https://discuss.elastic.co/u/prashant5375)
#### Post date: [March 15, 2015, 6:04pm UTC](https://discuss.elastic.co/t/joining-multiple-aggregration-queries-into-one-query/22673/2 "2015-03-15T18:04:39Z")

</div>

Adding Reference to it.

> <https://gist.github.com/parasb/751baf0fca2a7682a8d1>
>
> There are more than three files. show original

On Sunday, March 15, 2015 at 11:32:20 PM UTC+5:30, Prashant singh wrote:

> Hi ,  
> I have One query given below .
> 
> * * *
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "query\_string": {  
> "query": "\*"  
> }  
> },  
> "filter": {  
> "bool": {  
> "should": [  
> {  
> "fquery": {  
> "query": {  
> "query\_string": {  
> "query": ""357539019562335""  
> }  
> },  
> "\_cache": true  
> }  
> },  
> {  
> "fquery": {  
> "query": {  
> "query\_string": {  
> "query": ""357539019562624""  
> }  
> },  
> "\_cache": true  
> }  
> }  
> ],  
> "must": [  
> {  
> "fquery": {  
> "query": {  
> "query\_string": {  
> "query": "SMSATTEMPTSMO + SMSATTEMPTSMT"  
> }  
> },  
> "\_cache": true  
> }  
> },  
> {  
> "range": {  
> "@timestamp": {  
> "to": "2015-03-05T00:00-05:00",  
> "from": "2015-03-04T00:00-05:00"  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> },  
> "aggs": {  
> "events\_by\_IMEI": {  
> "terms": {  
> "field": "IMEI"  
> },  
> "aggs": {  
> "sms\_over\_time": {  
> "date\_histogram": {  
> "field": "@timestamp",  
> "interval": "1h",  
> "post\_zone": "-05:00",  
> "min\_doc\_count": 0,  
> "extended\_bounds": {  
> "min": "2015-03-04T00:00-05:00",  
> "max": "2015-03-05T00:00-05:00"  
> }  
> },  
> "aggs": {  
> "total\_sms": {  
> "sum": {  
> "field": "value"  
> }  
> }  
> }  
> }  
> }  
> }  
> },  
> "size": 0  
> }  
> Which gives me below response.
> 
> * * *
> 
> ## { "took": 9279, "timed\_out": false, "\_shards": { "total": 20, "successful": 20, "failed": 0 }, "hits": { "total": 354, "max\_score": 0, "hits": }, "aggregations": { "events\_by\_IMEI": { "buckets": [{ "key": "357539019562624", "doc\_count": 178, "sms\_over\_time": { "buckets": [ { "key\_as\_string": "2015-03-04T00:00:00.000Z", "key": 1425427200000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T01:00:00.000Z", "key": 1425430800000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T02:00:00.000Z", "key": 1425434400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T03:00:00.000Z", "key": 1425438000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T04:00:00.000Z", "key": 1425441600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T05:00:00.000Z", "key": 1425445200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T06:00:00.000Z", "key": 1425448800000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T07:00:00.000Z", "key": 1425452400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T08:00:00.000Z", "key": 1425456000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T09:00:00.000Z", "key": 1425459600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T10:00:00.000Z", "key": 1425463200000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T11:00:00.000Z", "key": 1425466800000, "doc\_count": 8, "total\_sms": { "value": 8 } }, { "key\_as\_string": "2015-03-04T12:00:00.000Z", "key": 1425470400000, "doc\_count": 8, "total\_sms": { "value": 3 } }, { "key\_as\_string": "2015-03-04T13:00:00.000Z", "key": 1425474000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T14:00:00.000Z", "key": 1425477600000, "doc\_count": 8, "total\_sms": { "value": 2 } }, { "key\_as\_string": "2015-03-04T15:00:00.000Z", "key": 1425481200000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T16:00:00.000Z", "key": 1425484800000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T17:00:00.000Z", "key": 1425488400000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T18:00:00.000Z", "key": 1425492000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T19:00:00.000Z", "key": 1425495600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T20:00:00.000Z", "key": 1425499200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T21:00:00.000Z", "key": 1425502800000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T22:00:00.000Z", "key": 1425506400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T23:00:00.000Z", "key": 1425510000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-05T00:00:00.000Z", "key": 1425513600000, "doc\_count": 2, "total\_sms": { "value": 0 } }] } }, { "key": "357539019562335", "doc\_count": 176, "sms\_over\_time": { "buckets": [{ "key\_as\_string": "2015-03-04T00:00:00.000Z", "key": 1425427200000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T01:00:00.000Z", "key": 1425430800000, "doc\_count": 0, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T02:00:00.000Z", "key": 1425434400000, "doc\_count": 6, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T03:00:00.000Z", "key": 1425438000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T04:00:00.000Z", "key": 1425441600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T05:00:00.000Z", "key": 1425445200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T06:00:00.000Z", "key": 1425448800000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T07:00:00.000Z", "key": 1425452400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T08:00:00.000Z", "key": 1425456000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T09:00:00.000Z", "key": 1425459600000, "doc\_count": 8, "total\_sms": { "value": 3 } }, { "key\_as\_string": "2015-03-04T10:00:00.000Z", "key": 1425463200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T11:00:00.000Z", "key": 1425466800000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T12:00:00.000Z", "key": 1425470400000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T13:00:00.000Z", "key": 1425474000000, "doc\_count": 8, "total\_sms": { "value": 3 } }, { "key\_as\_string": "2015-03-04T14:00:00.000Z", "key": 1425477600000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T15:00:00.000Z", "key": 1425481200000, "doc\_count": 8, "total\_sms": { "value": 10 } }, { "key\_as\_string": "2015-03-04T16:00:00.000Z", "key": 1425484800000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T17:00:00.000Z", "key": 1425488400000, "doc\_count": 8, "total\_sms": { "value": 4 } }, { "key\_as\_string": "2015-03-04T18:00:00.000Z", "key": 1425492000000, "doc\_count": 8, "total\_sms": { "value": 2 } }, { "key\_as\_string": "2015-03-04T19:00:00.000Z", "key": 1425495600000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T20:00:00.000Z", "key": 1425499200000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T21:00:00.000Z", "key": 1425502800000, "doc\_count": 8, "total\_sms": { "value": 1 } }, { "key\_as\_string": "2015-03-04T22:00:00.000Z", "key": 1425506400000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-04T23:00:00.000Z", "key": 1425510000000, "doc\_count": 8, "total\_sms": { "value": 0 } }, { "key\_as\_string": "2015-03-05T00:00:00.000Z", "key": 1425513600000, "doc\_count": 2, "total\_sms": { "value": 0 } }] } } ] } } }
> 
> So my question here is if i have to combine 2 or more such queries and expect the response in different-2 buckets of buckets , how  
> would we do that.

--  
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/35dfbc81-a589-4b96-939a-3ceb5fecfa80%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/35dfbc81-a589-4b96-939a-3ceb5fecfa80%40googlegroups.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:26am UTC](https://discuss.elastic.co/t/joining-multiple-aggregration-queries-into-one-query/22673/3 "2017-07-06T00:26:41Z")

</div>


