# Elastic Search Nested Aggregations

**URL:** <https://discuss.elastic.co/t/elastic-search-nested-aggregations/131588>\
**Category:** Elasticsearch\
**Created:** [May 12, 2018, 5:54pm UTC](https://discuss.elastic.co/t/elastic-search-nested-aggregations/131588 "2018-05-12T17:54:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![onlinejava](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/onlinejava/32/45359_2.png) [@onlinejava](https://discuss.elastic.co/u/onlinejava)\
**Post date:** [May 12, 2018, 5:54pm UTC](https://discuss.elastic.co/t/elastic-search-nested-aggregations/131588/1 "2018-05-12T17:54:22Z")

</div>

Hi  
i am new to elastic search Query and aggregation.  
I have a nested document with the following mapping

PUT /company  
{  
"mappings": {  
"data": {  
"properties": {  
"deptId": {  
"type": "keyword"  
},  
"deptName": {  
"type": "keyword"  
},  
"employee": {  
"type": "nested",  
"properties": {  
"empId": {  
"type": "keyword"  
},  
"empName": {  
"type": "text"  
},  
"salary": {  
"type": "float"  
}}  
} } } }}

I have sample data like

PUT company/data/1  
{  
"deptId":"1",  
"deptName":"HR",  
"employee": [  
{  
"empId": "1",  
"empName": "John",  
"salary":"1000"  
},  
{  
"empId": "2",  
"empName": "Will",  
"salary":"2000"  
}  
]  
}

PUT company/data/2  
{  
"deptId":"2",  
"deptName":"FINANCE",  
"employee": [  
{  
"empId": "1",  
"empName": "John",  
"salary":"1000"  
}]  
}

PUT company/data/3  
{  
"deptId":"3",  
"deptName":"FINANCE",  
"employee": [  
{  
"empId": "1",  
"empName": "John",  
"salary":"1000"  
},  
{  
"empId": "2",  
"empName": "Will",  
"salary":"2000"  
},  
{  
"empId": "3",  
"empName": "Mark",  
"salary":"4000"  
}]  
}

I would like assisting in creating a Query to

1. Department with the maximum Employees
2. Employee that is present in most departments

---

<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 9, 2018, 5:54pm UTC](https://discuss.elastic.co/t/elastic-search-nested-aggregations/131588/2 "2018-06-09T17:54:24Z")

</div>

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