# Nested documents

**URL:** <https://discuss.elastic.co/t/nested-documents/7080>\
**Category:** Elasticsearch\
**Created:** [March 21, 2012, 9:17am UTC](https://discuss.elastic.co/t/nested-documents/7080 "2012-03-21T09:17:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_Jancarik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr_jancarik/32/93503_2.png) [@Petr\_Jancarik](https://discuss.elastic.co/u/Petr_Jancarik)\
**Post date:** [March 21, 2012, 9:17am UTC](https://discuss.elastic.co/t/nested-documents/7080/1 "2012-03-21T09:17:43Z")

</div>

Hi,

i have index "test" and type "test" with mappings:  
[http://pastebin.com/7uVNzBR7](http://pastebin.com/7uVNzBR7)

and given test data:  
[http://pastebin.com/xNsChspp](http://pastebin.com/xNsChspp)

When i try to search with nested filter on "nested\_docs.nested\_doc1"  
[http://pastebin.com/FJuLytjH](http://pastebin.com/FJuLytjH), the result is collection of documents,  
but when "path" is changed to "nested\_docs.nested\_doc2" empty result  
is returned. It is similar with using nested\_docs.nested\_doc3 and  
nested\_docs.nested\_doc4, only path with "nested\_docs.nested\_doc4"  
returns collection of documents. So it seems to me when 2 nested  
documents have same property name it is possible to query only one of  
them with successful results. Is it a bug?

Thanks

---

<div class="post-metadata">

**Author:** ![Petr\_Jancarik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr_jancarik/32/93503_2.png) [@Petr\_Jancarik](https://discuss.elastic.co/u/Petr_Jancarik)\
**Post date:** [March 22, 2012, 7:16am UTC](https://discuss.elastic.co/t/nested-documents/7080/2 "2012-03-22T07:16:47Z")

</div>

Could anyone please help me with this nested documents problem? I  
found only it works when each nested document has unique named  
properties, but i guess there is other way to sort it out?

On Mar 21, 10:17 am, Petr Jancarik [jancarikp...@gmail.com](mailto:jancarikp...@gmail.com) wrote:

> Hi,
> 
> i have index "test" and type "test" with mappings:[{ "test": { "properties": { "nested\_docs": { - Pastebin.com](http://pastebin.com/7uVNzBR7)
> 
> and given test data:[{ "name": "asasasa", "id": "789", "nested\_docs": { "nest - Pastebin.com](http://pastebin.com/xNsChspp)
> 
> When i try to search with nested filter on "nested\_docs.nested\_doc1"[{ "filter": { "nested": { "path": "nested\_docs.nested - Pastebin.com](http://pastebin.com/FJuLytjH), the result is collection of documents,  
> but when "path" is changed to "nested\_docs.nested\_doc2" empty result  
> is returned. It is similar with using nested\_docs.nested\_doc3 and  
> nested\_docs.nested\_doc4, only path with "nested\_docs.nested\_doc4"  
> returns collection of documents. So it seems to me when 2 nested  
> documents have same property name it is possible to query only one of  
> them with successful results. Is it a bug?
> 
> Thanks

---

<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, 10:32am UTC](https://discuss.elastic.co/t/nested-documents/7080/3 "2012-03-25T10:32:15Z")

</div>

You have here two level of nested mapping, nested\_docs, and then another  
one. So, you need to have two level of nested queries/filters to execute on  
the second level one. Though, I am not sure you really need the nested\_docs  
level to be nested type.

On Wed, Mar 21, 2012 at 11:17 AM, Petr Jancarik [jancarikpetr@gmail.com](mailto:jancarikpetr@gmail.com)wrote:

> Hi,
> 
> i have index "test" and type "test" with mappings:  
> [{ "test": { "properties": { "nested\_docs": { - Pastebin.com](http://pastebin.com/7uVNzBR7)
> 
> and given test data:  
> [{ "name": "asasasa", "id": "789", "nested\_docs": { "nest - Pastebin.com](http://pastebin.com/xNsChspp)
> 
> When i try to search with nested filter on "nested\_docs.nested\_doc1"  
> [{ "filter": { "nested": { "path": "nested\_docs.nested - Pastebin.com](http://pastebin.com/FJuLytjH), the result is collection of documents,  
> but when "path" is changed to "nested\_docs.nested\_doc2" empty result  
> is returned. It is similar with using nested\_docs.nested\_doc3 and  
> nested\_docs.nested\_doc4, only path with "nested\_docs.nested\_doc4"  
> returns collection of documents. So it seems to me when 2 nested  
> documents have same property name it is possible to query only one of  
> them with successful results. Is it a bug?
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![Petr\_Jancarik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr_jancarik/32/93503_2.png) [@Petr\_Jancarik](https://discuss.elastic.co/u/Petr_Jancarik)\
**Post date:** [March 25, 2012, 8:04pm UTC](https://discuss.elastic.co/t/nested-documents/7080/4 "2012-03-25T20:04:49Z")

</div>

Thanks for tip, but it didn´t sort the problem out. I tried 2 level nested  
queries then i remapping index and kept nested\_docs without nested mapping

- same result like before. Finally i created index without nested\_docs at  
all, so every nested doc was directly in the root document, but results  
were same again. It is really strange, because only query on nested\_doc1  
and nested\_doc4 are successful. Why not nested\_doc2 and nested\_doc3?

On Sunday, March 25, 2012 12:32:15 PM UTC+2, kimchy wrote:

> You have here two level of nested mapping, nested\_docs, and then another  
> one. So, you need to have two level of nested queries/filters to execute on  
> the second level one. Though, I am not sure you really need the nested\_docs  
> level to be nested type.
> 
> On Wed, Mar 21, 2012 at 11:17 AM, Petr Jancarik wrote:
> 
> > Hi,
> > 
> > i have index "test" and type "test" with mappings:  
> > [{ "test": { "properties": { "nested\_docs": { - Pastebin.com](http://pastebin.com/7uVNzBR7)
> > 
> > and given test data:  
> > [{ "name": "asasasa", "id": "789", "nested\_docs": { "nest - Pastebin.com](http://pastebin.com/xNsChspp)
> > 
> > When i try to search with nested filter on "nested\_docs.nested\_doc1"  
> > [{ "filter": { "nested": { "path": "nested\_docs.nested - Pastebin.com](http://pastebin.com/FJuLytjH), the result is collection of documents,  
> > but when "path" is changed to "nested\_docs.nested\_doc2" empty result  
> > is returned. It is similar with using nested\_docs.nested\_doc3 and  
> > nested\_docs.nested\_doc4, only path with "nested\_docs.nested\_doc4"  
> > returns collection of documents. So it seems to me when 2 nested  
> > documents have same property name it is possible to query only one of  
> > them with successful results. Is it a bug?
> > 
> > Thanks

---

<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 27, 2012, 1:00pm UTC](https://discuss.elastic.co/t/nested-documents/7080/5 "2012-03-27T13:00:27Z")

</div>

Can you gist a recreation with one level nested mapping? I will have a look.

On Sun, Mar 25, 2012 at 10:04 PM, Petr Jancarik [jancarikpetr@gmail.com](mailto:jancarikpetr@gmail.com)wrote:

> Thanks for tip, but it didn´t sort the problem out. I tried 2 level nested  
> queries then i remapping index and kept nested\_docs without nested mapping
> 
> - same result like before. Finally i created index without nested\_docs at  
> all, so every nested doc was directly in the root document, but results  
> were same again. It is really strange, because only query on nested\_doc1  
> and nested\_doc4 are successful. Why not nested\_doc2 and nested\_doc3?
> 
> On Sunday, March 25, 2012 12:32:15 PM UTC+2, kimchy wrote:
> 
> > You have here two level of nested mapping, nested\_docs, and then another  
> > one. So, you need to have two level of nested queries/filters to execute on  
> > the second level one. Though, I am not sure you really need the nested\_docs  
> > level to be nested type.
> > 
> > On Wed, Mar 21, 2012 at 11:17 AM, Petr Jancarik wrote:
> > 
> > Hi,
> > 
> > > i have index "test" and type "test" with mappings:  
> > > [{ "test": { "properties": { "nested\_docs": { - Pastebin.com](http://pastebin.com/7uVNzBR7)
> > > 
> > > and given test data:  
> > > [{ "name": "asasasa", "id": "789", "nested\_docs": { "nest - Pastebin.com](http://pastebin.com/xNsChspp)
> > > 
> > > When i try to search with nested filter on "nested\_docs.nested\_doc1"  
> > > [{ "filter": { "nested": { "path": "nested\_docs.nested - Pastebin.com](http://pastebin.com/FJuLytjH), the result is collection of documents,  
> > > but when "path" is changed to "nested\_docs.nested\_doc2" empty result  
> > > is returned. It is similar with using nested\_docs.nested\_doc3 and  
> > > nested\_docs.nested\_doc4, only path with "nested\_docs.nested\_doc4"  
> > > returns collection of documents. So it seems to me when 2 nested  
> > > documents have same property name it is possible to query only one of  
> > > them with successful results. Is it a bug?
> > > 
> > > Thanks

---

<div class="post-metadata">

**Author:** ![Petr\_Jancarik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr_jancarik/32/93503_2.png) [@Petr\_Jancarik](https://discuss.elastic.co/u/Petr_Jancarik)\
**Post date:** [March 27, 2012, 2:18pm UTC](https://discuss.elastic.co/t/nested-documents/7080/6 "2012-03-27T14:18:49Z")

</div>

Finally i found the way how to query nested documents. It wasn´t clear in  
documentation. For addressing a field in nested query i used full path "  
nested\_docs.nested\_doc2.user\_id".

Here is mapping and search query i used [https://gist.github.com/2216182](https://gist.github.com/2216182)

Thank you

---

<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/nested-documents/7080/7 "2017-07-06T03:34:36Z")

</div>

Closed on July 6, 2017, 3:34am UTC


