# Nested aggregation query for max number of employees

**URL:** <https://discuss.elastic.co/t/nested-aggregation-query-for-max-number-of-employees/156236>\
**Category:** Elasticsearch\
**Created:** [November 12, 2018, 6:47am UTC](https://discuss.elastic.co/t/nested-aggregation-query-for-max-number-of-employees/156236 "2018-11-12T06:47:15Z")\
**Posts on this page:** 2\
**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:** [November 12, 2018, 6:47am UTC](https://discuss.elastic.co/t/nested-aggregation-query-for-max-number-of-employees/156236/1 "2018-11-12T06:47:15Z")

</div>

Hi All,

I have a below scenario of requirement.  
Need top 10 companies with maximum number of employees.  
Below is my Elasticsearch query without nested fields.

```
{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "groupby": {
            "composite": {
                "size": 10,
                "sources": [
                    {
                        "269": {
                            "terms": {
                                "field": "typeCode",
                                "missing_bucket": false,
                                "order": "asc"
                            }
                        }
                    }
                ]
            }
        }
    }
}

```

Above query is working fine. But I need this to be working with nested field i.e.,

```
{
	"size": 0,
	"_source": false,
	"stored_fields": "_none_",
	"aggs": {
		"industryCodes_path": {
			"nested": {
				"path": "codeTypes"
			},
			"aggregations": {
				"groupby": {
					"composite": {
						"size": 10,
						"sources": [{
							"860": {
								"terms": {
									"field": "typeCode",
									"order": "asc"
								}
							}
						}]
					}
				}
			}
		}
	}
}

```

Above is with nested field, which is failing with below error message,

> [composite] aggregation cannot be used with a parent aggregation

Could someone please help me on how to solve this issue.

Thanks,  
Ram G

---

<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:** [December 10, 2018, 6:47am UTC](https://discuss.elastic.co/t/nested-aggregation-query-for-max-number-of-employees/156236/2 "2018-12-10T06:47:29Z")

</div>

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