# Range aggregation query inside each range aggregation

**URL:** https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653
**Category:** Elasticsearch
**Created:** [July 19, 2018, 6:06am UTC](https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653 "2018-07-19T06:06:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [July 19, 2018, 6:06am UTC](https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653/1 "2018-07-19T06:06:30Z")

</div>

Hi All,

I have a requirement like, I need to get the documents count for two fields based on the given ranges. Like, first field having ranges like 0-50,51-100 and second field having ranges like 0-500,501-1000. Now, as we get documents count for each range of first field along with that for each range of first field we need to get documents count for each range of second field.  
Below is my sample query.

> ```
> {
> "aggs": {
> "nested_aggr_noofemp": {
> "nested": {
> "path": "numberOfEmployees"
> },
> "aggs": {
> "noofemp_ranges": {
> "range": {
> "field": "numberOfEmployees.value",
> "ranges": [{
> "to": 100
> },
> {
> "from": 100,
> "to": 200
> },
> {
> "from": 200
> }
> ]
> },
> "nested_aggr_solvencyRatio": {
> "nested": {
> "path": "standardisedViewReferenceCurrency"
> },
> "aggs": {
> "solvencyRatio_range": {
> "range": {
> "field": "standardisedViewReferenceCurrency.derivedFinancialItems.solvencyRatio",
> "ranges": [{
> "to": 50
> },
> {
> "from": 51,
> "to": 100
> },
> {
> "from": 101
> }
> ]
> }
> }
> }
> }
> }
> }
> }
> }
> }
> 
> ```

But with the above query, I am getting below error msg:

> {  
> "error": {  
> "root\_cause": [  
> {  
> "type": "parsing\_exception",  
> "reason": "Found two aggregation type definitions in [noofemp\_ranges]: [range] and [nested\_aggr\_solvencyRatio]",  
> "line": 23,  
> "col": 35  
> }  
> ],  
> "type": "parsing\_exception",  
> "reason": "Found two aggregation type definitions in [noofemp\_ranges]: [range] and [nested\_aggr\_solvencyRatio]",  
> "line": 23,  
> "col": 35  
> },  
> "status": 400  
> }

Please let me know where I did mistake.

Thanks,  
Ram Prasad G

---

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [July 19, 2018, 7:12am UTC](https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653/2 "2018-07-19T07:12:52Z")

</div>

A small update which fixed the above issue, below is the updated query,

> ```
> {
> "aggs": {
> "nested_aggr_noofemp": {
> "nested": {
> "path": "standardisedFinancials.identification.numberOfEmployees"
> },
> "aggs": {
> "noofemp_ranges": {
> "range": {
> "field": "standardisedFinancials.identification.numberOfEmployees.value",
> "ranges": [{
> "to": 100
> },
> {
> "from": 100,
> "to": 200
> },
> {
> "from": 200
> }
> ]
> },
> "aggs": {
> "nested_aggr_solvencyRatio": {
> "nested": {
> "path": "standardisedFinancials.financials.latestFinancials.standardisedViewReferenceCurrency"
> },
> "aggs": {
> "solvencyRatio_range": {
> "range": {
> "field": "standardisedFinancials.financials.latestFinancials.standardisedViewReferenceCurrency.derivedFinancialItems.solvencyRatio",
> "ranges": [{
> "to": 50
> },
> {
> "from": 51,
> "to": 100
> },
> {
> "from": 101
> }
> ]
> }
> }
> }
> }
> }
> }
> }
> }
> }
> }
> 
> ```

But the above query giving wrong results, like count 0 for sub-bucket ranges.  
Kindly help me with my query please.

Thanks,  
Ram Prasad G

---

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [July 20, 2018, 9:22am UTC](https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653/3 "2018-07-20T09:22:46Z")

</div>

Any suggestions please.

---

<div class="post-metadata">

### Author: ![prasad.ram1431](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@prasad.ram1431](https://discuss.elastic.co/u/prasad.ram1431)
#### Post date: [July 30, 2018, 5:44am UTC](https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653/4 "2018-07-30T05:44:59Z")

</div>

Hello does anyone got chance to look into this issue. I need this, as I got stuck with my implementation. Please please help me.

---

<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: [August 27, 2018, 5:45am UTC](https://discuss.elastic.co/t/range-aggregation-query-inside-each-range-aggregation/140653/5 "2018-08-27T05:45:01Z")

</div>

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