# Group by aggregation on nested document

**URL:** <https://discuss.elastic.co/t/group-by-aggregation-on-nested-document/239439>\
**Category:** Elasticsearch\
**Created:** [July 1, 2020, 8:35am UTC](https://discuss.elastic.co/t/group-by-aggregation-on-nested-document/239439 "2020-07-01T08:35:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidker](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@davidker](https://discuss.elastic.co/u/davidker)\
**Post date:** [July 1, 2020, 8:35am UTC](https://discuss.elastic.co/t/group-by-aggregation-on-nested-document/239439/1 "2020-07-01T08:35:13Z")

</div>

Let's say I have five document with some nested data:  
Doc 1 :

> ```
> {
> id:1,
> facilities:[
> {
> type:"hospital"
> }   
> ],
> investigator:[{
> name"Robert"
> },
> {
> name:"Julie"
> }
> }],
> }    
> 
> ```

Doc 2

> ```
> {
> id:2,
> facilities:[
> {
> type:"medical"
> }   
> ],
> investigator:[{
> name"Robert"
> },
> {
> name:"Julie"
> }
> }],
> }   
> 
> ```

```
Doc 3:
{
     id:3,
    facilities:[
     {
         type:"biological"
     }   
     ],
     investigator:[{
          name"David"
            },
          {
             name:"Julie"
         }
     }],
 }    

```

I want to search all document where the investigator name is "Robert" and I want it to return like this:

> {  
> investigator\_name:"Robert",  
> id\_list:[{id:"1"} , {id:"2"}],  
> facilities\_list:[{ type:"hospital"},{ type:"medical"}]
> 
> }

Is that even possible?

---

<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 29, 2020, 8:35am UTC](https://discuss.elastic.co/t/group-by-aggregation-on-nested-document/239439/2 "2020-07-29T08:35:16Z")

</div>

Closed on July 29, 2020, 8:35am UTC

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