# Unable to use nested query on elastic search 7.2.0

**URL:** <https://discuss.elastic.co/t/unable-to-use-nested-query-on-elastic-search-7-2-0/191007>\
**Category:** Elasticsearch\
**Created:** [July 17, 2019, 1:29pm UTC](https://discuss.elastic.co/t/unable-to-use-nested-query-on-elastic-search-7-2-0/191007 "2019-07-17T13:29:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimy\_page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimy_page/32/47716_2.png) [@jimy\_page](https://discuss.elastic.co/u/jimy_page)\
**Post date:** [July 17, 2019, 1:29pm UTC](https://discuss.elastic.co/t/unable-to-use-nested-query-on-elastic-search-7-2-0/191007/1 "2019-07-17T13:29:53Z")

</div>

Hi I Just started using Elastic search and need to come up with a solution for nested queries.  
I was going through an older post but unable to get the nested query right.

Let's say I have document which has user arrays, inside that I have cars array.  
I want to be able to query something like this : find documents where user.usertype="employed" and user.cars.cartype="suv".

Here is the sample mapping and queries:

PUT [http://localhost:9200/t](http://localhost:9200/t)  
{  
"mappings": {  
"properties": {  
"t": {  
"type": "nested",  
"properties": {  
"categories": {  
"type": "nested",  
"properties": {  
"name": {  
"type": "text"  
},  
"list": {  
"type": "nested",  
"properties": {  
"url\_site": {  
"type": "text"  
},  
"persons": {  
"type": "nested",  
"properties": {  
"total\_customers": {  
"type": "integer"  
},  
"total\_subscribers": {  
"type": "integer"  
},  
"details": {  
"type": "nested",  
"properties": {  
"person\_id": {  
"type": "text"  
},  
"person\_date\_registration": {  
"type": "date"  
},  
"person\_date\_subscription": {  
"type": "date"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

PUT [http://localhost:9200/t/\_doc/1](http://localhost:9200/t/_doc/1)  
{  
"categories" : {  
"name" : "cat1",  
"list" : {  
"url\_site" : "[www.bla.org](http://www.bla.org)",  
"persons" : {  
"total\_customers" : 10,  
"total\_subscribers" : 10,  
"details" : {  
"person\_id" : 1  
}  
}  
}  
}  
}

PUT [http://localhost:9200/t/\_doc/2](http://localhost:9200/t/_doc/2)  
{  
"categories" : {  
"name" : "cat2",  
"list" : {  
"url\_site" : "[www.bleep.org](http://www.bleep.org)",  
"persons" : {  
"total\_customers" : 10,  
"total\_subscribers" : 10,  
"details" : {  
"person\_id" : 2  
}  
}  
}  
}  
}

PUT [http://localhost:9200/t/\_doc/3](http://localhost:9200/t/_doc/3)  
{  
"categories" : {  
"name" : "cat3",  
"list" : {  
"url\_site" : "[www.blubb.org](http://www.blubb.org)",  
"persons" : {  
"total\_customers" : 10,  
"total\_subscribers" : 10,  
"details" : {  
"person\_id" : 3  
}  
}  
}  
}  
}

GET [http://localhost:9200/t/\_search](http://localhost:9200/t/_search)  
{  
"query": {  
"nested": {  
"path": "categories",  
"query": {  
"nested": {  
"path": "categories.list",  
"query": {  
"nested": {  
"path": "categories.list.persons.details",  
"query": {  
"bool": {  
"must": {  
"match": {  
"categories.list.persons.details.person\_id": 1  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

curl -X GET "localhost:9200/t/\_search" -H 'Content-Type: application/json' -d'  
{  
"query": {  
"nested": {  
"path": "categories",  
"query": {  
"nested": {  
"path": "categories.list",  
"query": {  
"nested": {  
"path": "categories.list.persons.details",  
"query": {  
"bool": {  
"must": {  
"match": {  
"categories.list.persons.details.person\_id": 1  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
'

---

<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:** [August 14, 2019, 1:29pm UTC](https://discuss.elastic.co/t/unable-to-use-nested-query-on-elastic-search-7-2-0/191007/2 "2019-08-14T13:29:59Z")

</div>

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