# Query both root and nested documents

**URL:** <https://discuss.elastic.co/t/query-both-root-and-nested-documents/7087>\
**Category:** Elasticsearch\
**Created:** [March 21, 2012, 8:11pm UTC](https://discuss.elastic.co/t/query-both-root-and-nested-documents/7087 "2012-03-21T20:11:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![my3sons](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@my3sons](https://discuss.elastic.co/u/my3sons)\
**Post date:** [March 21, 2012, 8:11pm UTC](https://discuss.elastic.co/t/query-both-root-and-nested-documents/7087/1 "2012-03-21T20:11:48Z")

</div>

Hello,

Below is the gist related to my question.

> <https://gist.github.com/my3sons/2152146>

I have set up a sample index that contains a couple simple documents that contain a nested type. In addition, I have created a facet that only contains those filtered nested items that relate to the nested filter I have applied. So basically everything is working as I would like, except that in addition to the stores.id filter, I would like to query against the name and/or desc field (i.e. name=samsung), which is at the root level. I have tried a number of different attempts at this, but with no luck. In addition to the gist, I have copied one of the documents below, as well as the query I am using as of now, which is working with just the filter.

curl -X POST "[http://localhost:9200/deals\_near\_me/deals](http://localhost:9200/deals_near_me/deals)" -d '{  
"skuid": "123456",  
"name": "Samsung 55 inch TV",  
"desc": "Samsung Smart HDTV that really looks nice and is super thin",  
"price2" : 300,  
"stores":  
[  
{ "id": "1001", "price": 599, "inv\_status" : "avail" },  
{ "id": "1002", "price": 699, "inv\_status" : "avail" },  
{ "id": "1003", "price": 799, "inv\_status" : "avail" }  
]  
}'

curl 'localhost:9200/deals\_near\_me/deals/\_search?pretty=1' -d '{  
"query": {  
"nested": {  
"\_scope": "my\_scope",  
"path": "stores",  
"score\_mode": "avg",  
"query": {  
"filtered" : {  
"query" : {  
"match\_all": {}  
},  
"filter" : {  
"term" : { "stores.id" : "1001" }  
}  
}  
}  
}  
},  
"facets" : {  
"range1" : {  
"range" : {  
"stores.price" : [  
{ "to" : 200.0 },  
{ "from" : 200.0, "to" : 500.0 },  
{ "from" : 500.1, "to" : 700.0 },  
{ "from" : 700, "to" : 900.0 },  
{ "from" : 900, "to" : 1000.0 },  
{ "from" : 1000.0 }  
]  
},  
"scope": "my\_scope"  
}  
}  
}'

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 25, 2012, 11:24am UTC](https://discuss.elastic.co/t/query-both-root-and-nested-documents/7087/2 "2012-03-25T11:24:07Z")

</div>

You can place the nested query within a bool query for example, and have  
additional queries in this case working "against" the root level doc.

On Wed, Mar 21, 2012 at 10:11 PM, my3sons [carey.boldenow@gmail.com](mailto:carey.boldenow@gmail.com) wrote:

> Hello,
> 
> Below is the gist related to my question.
> 
> [query root and nested documents · GitHub](https://gist.github.com/2152146)
> 
> I have set up a sample index that contains a couple simple documents that  
> contain a nested type. In addition, I have created a facet that only  
> contains those filtered nested items that relate to the nested filter I  
> have  
> applied. So basically everything is working as I would like, except that in  
> addition to the stores.id filter, I would like to query against the name  
> and/or desc field (i.e. name=samsung), which is at the root level. I have  
> tried a number of different attempts at this, but with no luck. In addition  
> to the gist, I have copied one of the documents below, as well as the query  
> I am using as of now, which is working with just the filter.
> 
> curl -X POST "[http://localhost:9200/deals\_near\_me/deals](http://localhost:9200/deals_near_me/deals)" -d '{  
> "skuid": "123456",  
> "name": "Samsung 55 inch TV",  
> "desc": "Samsung Smart HDTV that really looks nice and is super thin",  
> "price2" : 300,  
> "stores":  
> [  
> { "id": "1001", "price": 599, "inv\_status" : "avail" },  
> { "id": "1002", "price": 699, "inv\_status" : "avail" },  
> { "id": "1003", "price": 799, "inv\_status" : "avail" }  
> ]  
> }'
> 
> curl 'localhost:9200/deals\_near\_me/deals/\_search?pretty=1' -d '{  
> "query": {  
> "nested": {  
> "\_scope": "my\_scope",  
> "path": "stores",  
> "score\_mode": "avg",  
> "query": {  
> "filtered" : {  
> "query" : {  
> "match\_all": {}  
> },  
> "filter" : {  
> "term" : { "stores.id" : "1001" }  
> }  
> }  
> }  
> }  
> },  
> "facets" : {  
> "range1" : {  
> "range" : {  
> "stores.price" : [  
> { "to" : 200.0 },  
> { "from" : 200.0, "to" : 500.0 },  
> { "from" : 500.1, "to" : 700.0 },  
> { "from" : 700, "to" : 900.0 },  
> { "from" : 900, "to" : 1000.0 },  
> { "from" : 1000.0 }  
> ]  
> },  
> "scope": "my\_scope"  
> }  
> }  
> }'
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/query-both-root-and-nested-documents-tp3846688p3846688.html](http://elasticsearch-users.115913.n3.nabble.com/query-both-root-and-nested-documents-tp3846688p3846688.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 3:34am UTC](https://discuss.elastic.co/t/query-both-root-and-nested-documents/7087/3 "2017-07-06T03:34:56Z")

</div>


