# Nested Query

**URL:** <https://discuss.elastic.co/t/nested-query/14747>\
**Category:** Elasticsearch\
**Created:** [December 6, 2013, 9:31am UTC](https://discuss.elastic.co/t/nested-query/14747 "2013-12-06T09:31:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yogesh\_Totare](https://avatars.discourse-cdn.com/v4/letter/y/a5b964/32.png) [@Yogesh\_Totare](https://discuss.elastic.co/u/Yogesh_Totare)\
**Post date:** [December 6, 2013, 9:31am UTC](https://discuss.elastic.co/t/nested-query/14747/1 "2013-12-06T09:31:49Z")

</div>

I am new to the elasticsearch. Help me to resolve the issue..

I have a following mapping file. Which contain the **companyId** as property for the nested documents like **forComment** , **from** , **forInfo** , **assignee** and **comments**

mappings: {  
approval: {  
properties: {  
**forComment** : {  
properties: {  
**companyId** : {  
type: long  
}  
companyName: {  
type: string  
}  
}  
type: nested  
}  
submittalRefNumber: {  
type: string  
}  
project\_id: {  
type: long  
}

```
            <b>from</b>: {
                properties: {
                    <b>companyId</b>: {
                        type: long
                    }
                    companyName: {
                        type: string
                    }
                }
                type: nested
            }
           <b>forInfo</b>: {
                properties: {
                    <b>companyId</b>: {
                        type: long
                    }
                    companyName: {
                        type: string
                    }
                }
                type: nested
            }
           first_version_id: {
                type: long
            }
            <b>assignee</b>: {
                properties: {
                    <b>companyId</b>: {
                        type: long
                    }
                    companyName: {
                        type: string
                    }
                }
                type: nested
            }
           
            <b>comments</b>: {
                properties: {
                    <b>companyId</b>: {
                        type: long
                    }
                    companyName: {
                        type: string
                    }
                    commentId: {
                        type: long
                    }
                }
                type: nested
            }
        }
    }
}
aliases: []

```

}

the issue i am facing is to find out the number of records where assignee.companyId = X. when i fire the bellow query i get all the records where companyId = X which includes record from comments, forInfo, assignee, from and forComment. How can i get the specific records from assignee only?

{

"size": 20,  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"project\_id": 13828  
}  
},  
{  
"nested": {  
"path": "assignee",  
"query": {  
"term": {  
"assignee.companyId": "1223"  
}  
}  
}  
}  
]  
}  
}  
}

---

<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 6, 2017, 2:02am UTC](https://discuss.elastic.co/t/nested-query/14747/2 "2017-07-06T02:02:53Z")

</div>

Closed on July 6, 2017, 2:02am UTC


