# Query on multiple nested objects

**URL:** https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423
**Category:** Elasticsearch
**Created:** [April 2, 2013, 11:37pm UTC](https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423 "2013-04-02T23:37:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Victor\_Huang](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@Victor\_Huang](https://discuss.elastic.co/u/Victor_Huang)
#### Post date: [April 2, 2013, 11:37pm UTC](https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423/1 "2013-04-02T23:37:52Z")

</div>

a have Author model

Author: {  
name: xyz,  
books: [ { //nested object  
title: "abc",  
date: 1/1/1  
},{  
title: "123"  
date: 2/2/2  
}  
],  
deals: [{ //nested object  
name: "best",  
deal\_end\_at: 1/1/15  
},  
{  
name: "not so good",  
deal\_end\_at: 1/2/13  
}

]  
}

is it possible to construct a single query to find the author where  
"author.book.title = 'abc'" and "deals.name = 'best' "?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [April 3, 2013, 9:54am UTC](https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423/2 "2013-04-03T09:54:20Z")

</div>

Yes, this is possible by using a match query for your title query and a  
nested query that wraps a match query for the deal names. Both the nested  
and match query need to combined with a bool query. Example:  
curl -XGET 'localhost:9200/\_search' -d '  
"query" : {  
"bool" : {  
"must" : [  
{  
"match" : {  
"name" : {  
"abc"  
}  
}  
},  
"nested" : {  
"path" : "deals",  
"query" : {  
"match" : {  
"name" : {  
"best"  
}  
}  
}  
}  
]  
}  
}  
'

On 3 April 2013 01:37, Victor [vicwin@gmail.com](mailto:vicwin@gmail.com) wrote:

> a have Author model
> 
> Author: {  
> name: xyz,  
> books: [ { //nested object  
> title: "abc",  
> date: 1/1/1  
> },{  
> title: "123"  
> date: 2/2/2  
> }  
> ],  
> deals: [{ //nested object  
> name: "best",  
> deal\_end\_at: 1/1/15  
> },  
> {  
> name: "not so good",  
> deal\_end\_at: 1/2/13  
> }
> 
> ]  
> }
> 
> is it possible to construct a single query to find the author where  
> "author.book.title = 'abc'" and "deals.name = 'best' "?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Victor\_Huang](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@Victor\_Huang](https://discuss.elastic.co/u/Victor_Huang)
#### Post date: [April 3, 2013, 5:16pm UTC](https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423/3 "2013-04-03T17:16:33Z")

</div>

in the first match query, how does ES knows "name" mapped to  
"books.title"? am i missing something

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [April 3, 2013, 5:46pm UTC](https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423/4 "2013-04-03T17:46:55Z")

</div>

ES doesn't know that. I misread your question / example. The book.title  
part should also be put into a nested query:  
curl -XGET 'localhost:9200/\_search' -d '  
"query" : {  
"bool" : {  
"must" : [  
{  
"nested" : {  
"path" : "books",  
"query" : {  
"match" : {  
"books.title" : {  
"abc"  
}  
}  
}  
}  
},  
"nested" : {  
"path" : "deals",  
"query" : {  
"match" : {  
"deals.name" : {  
"best"  
}  
}  
}  
}  
]  
}  
}  
'

Also the field names inside the nested query should be the full path. The  
above query should return any root document (author in your case) that has  
a nested inner deal object with a name that has the term `best` and a  
nested inner book object with title that has a term `abc`. Does this makes  
sense?

On 3 April 2013 19:16, Victor Huang [vicwin@gmail.com](mailto:vicwin@gmail.com) wrote:

> in the first match query, how does ES knows "name" mapped to  
> "books.title"? am i missing something
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:42am UTC](https://discuss.elastic.co/t/query-on-multiple-nested-objects/11423/5 "2017-07-06T02:42:59Z")

</div>


