# Reverse nested aggs not working in group by nested and non fields

**URL:** <https://discuss.elastic.co/t/reverse-nested-aggs-not-working-in-group-by-nested-and-non-fields/233890>\
**Category:** Elasticsearch\
**Created:** [May 22, 2020, 10:59am UTC](https://discuss.elastic.co/t/reverse-nested-aggs-not-working-in-group-by-nested-and-non-fields/233890 "2020-05-22T10:59:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadlybeast0](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deadlybeast0](https://discuss.elastic.co/u/deadlybeast0)\
**Post date:** [May 22, 2020, 10:59am UTC](https://discuss.elastic.co/t/reverse-nested-aggs-not-working-in-group-by-nested-and-non-fields/233890/1 "2020-05-22T10:59:25Z")

</div>

Hi i am trying to do group by nested and non nested fields.I want to do group by on 1 non nested fields(from\_district) ,1 nested field(truck\_number) and max on nested field(truck\_number.score).

Requirement -: to get max score of each truck in all districts if truck is present in that district for a given sp\_id eg-:  
`District1 ,truck1, 0.9, District2 ,truck1, 0.8, District1 ,truck2, 1.8, District2 ,truck3, 0.7, District3 ,truck4, 1.7`  
Below is my mapping-:

`  
{  
"sp\_ranked\_indent" : {  
"mappings" : {  
"properties" : {  
"from\_district" : {  
"type" : "keyword"  
},  
"sp\_id" : {  
"type" : "long"  
},  
"to\_district" : {  
"type" : "keyword"  
},  
"truck\_ranking\_document" : {  
"type" : "nested",  
"properties" : {  
"score" : {  
"type" : "float"  
},  
"truck\_number" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
}  
}  
}  
}  
}  
}  
}

`

Below is the query that i tried but it is not grouping by nested and non nested field and also the max truck score is incorrect

`{ "size": 0, "query": { "terms": { "sp_id": [650128], "boost": 1.0 } }, "aggregations": { "NESTED_AGG": { "nested": { "path": "truck_ranking_document" }, "aggregations": { "max_score": { "max": { "field": "truck_ranking_document.score" } }, "truck_numer": { "terms": { "field": "truck_ranking_document.truck_number.keyword", "size": 10, "min_doc_count": 1, "shard_min_doc_count": 0, "show_term_doc_count_error": false, "order": [{ "_count": "desc" }, { "_key": "asc" }] } }, "fromdistrictagg": { "reverse_nested": {}, "aggregations": { "fromDistrict": { "terms": { "field": "from_district", "size": 10, "min_doc_count": 1, "shard_min_doc_count": 0, "show_term_doc_count_error": false, "order": [{ "_count": "desc" }, { "_key": "asc" }] } } } } } } } }`

---

<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:** [June 19, 2020, 10:59am UTC](https://discuss.elastic.co/t/reverse-nested-aggs-not-working-in-group-by-nested-and-non-fields/233890/2 "2020-06-19T10:59:43Z")

</div>

Closed on June 19, 2020, 10:59am UTC

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