# Multi-Leveled Nested Object Facet Search Requiring multiple Levels of Nested Object Access Does not access all fields

**URL:** https://discuss.elastic.co/t/multi-leveled-nested-object-facet-search-requiring-multiple-levels-of-nested-object-access-does-not-access-all-fields/13482
**Category:** Elasticsearch
**Created:** [September 5, 2013, 11:01pm UTC](https://discuss.elastic.co/t/multi-leveled-nested-object-facet-search-requiring-multiple-levels-of-nested-object-access-does-not-access-all-fields/13482 "2013-09-05T23:01:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jessica](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jessica](https://discuss.elastic.co/u/Jessica)
#### Post date: [September 5, 2013, 11:01pm UTC](https://discuss.elastic.co/t/multi-leveled-nested-object-facet-search-requiring-multiple-levels-of-nested-object-access-does-not-access-all-fields/13482/1 "2013-09-05T23:01:45Z")

</div>

I have a very complicated process I'm trying to complete which, to get the  
data I need requires the following object format:

Document with values, one value is a nested document. That nested document  
itself has a nested document:  
general example:  
Object do  
Keywords do nested object  
Name =\> 'string'  
Records do nested object  
RecName =\> 'string'  
RecCount =\> 'integer'  
end  
end  
end

I need to create a terms\_stats facet which will facet on the Keywords.name  
value, and will sum the Keywords.Records.RecCount values

I have an example for the documents and my queries/facet formatting at the  
following gist: [https://gist.github.com/anonymous/6457316](https://gist.github.com/anonymous/6457316)

In this gist, the last two queries show:

1. If I use nested:"keywords" then I can correctly facet on the  
Keywords.Name value,
2. However it won't let me access the RecCount value and changing the  
nested to keywords.records gives no facet results.  
\*I've also tried to have multiple nested values with both keywords and  
keywords.name, but it seems to only take whichever nested value I give last.

I really need to be able to access both levels of the documents in the same  
facet. I need to have by Name, the total RecCounts and my documents need to  
be set up this way for other users.

Please Help!

--J

--  
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: ![Jessica](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jessica](https://discuss.elastic.co/u/Jessica)
#### Post date: [September 6, 2013, 8:12pm UTC](https://discuss.elastic.co/t/multi-leveled-nested-object-facet-search-requiring-multiple-levels-of-nested-object-access-does-not-access-all-fields/13482/2 "2013-09-06T20:12:51Z")

</div>

Answer:

add :include\_in\_parent =\> 'true' for the sub-object, which does not have to  
be nested and by doing nested : Keywords, all levels will be included.

On Thursday, September 5, 2013 6:01:45 PM UTC-5, Jessica wrote:

> I have a very complicated process I'm trying to complete which, to get the  
> data I need requires the following object format:
> 
> Document with values, one value is a nested document. That nested document  
> itself has a nested document:  
> general example:  
> Object do  
> Keywords do nested object  
> Name =\> 'string'  
> Records do nested object  
> RecName =\> 'string'  
> RecCount =\> 'integer'  
> end  
> end  
> end
> 
> I need to create a terms\_stats facet which will facet on the Keywords.name  
> value, and will sum the Keywords.Records.RecCount values
> 
> I have an example for the documents and my queries/facet formatting at the  
> following gist: [Elasticsearch Multi-Layered Nested Object: Accessing multiple Nested Layers in a Facet Does not work · GitHub](https://gist.github.com/anonymous/6457316)
> 
> In this gist, the last two queries show:
> 
> 1. If I use nested:"keywords" then I can correctly facet on the  
> Keywords.Name value,
> 2. However it won't let me access the RecCount value and changing the  
> nested to keywords.records gives no facet results.  
> \*I've also tried to have multiple nested values with both keywords and  
> keywords.name, but it seems to only take whichever nested value I give  
> last.
> 
> I really need to be able to access both levels of the documents in the  
> same facet. I need to have by Name, the total RecCounts and my documents  
> need to be set up this way for other users.
> 
> Please Help!
> 
> --J

--  
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: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [September 6, 2013, 8:31pm UTC](https://discuss.elastic.co/t/multi-leveled-nested-object-facet-search-requiring-multiple-levels-of-nested-object-access-does-not-access-all-fields/13482/3 "2013-09-06T20:31:12Z")

</div>

Gah, sorry, I had a reply written up to this but apparently never hit  
submit. You're solution is correct and is really the only way to do it  
without restructuring your data.

Below is the response that I never posted, provides some extra background:

I'm afraid you won't be able to do it, with this current layout. It's a

> fundamental limitation to how nested documents are managed under the  
> surface.
> 
> Nested docs are basically "hidden" documents that are physically located  
> right next to the parent. They have their own internal Lucene ID and  
> behave just like documents. When you are doing queries or facets,  
> documents are handled one at a time. Think of it like a stream of  
> documents and you have a window of size 1. What this means is that you can  
> only "see" the data within that window, which is either the parent, the  
> child or the grand-child. You're window never holds two documents at the  
> same time, so you can never cross-relate data between parent and child.
> 
> Since you can only look at one document at a time, you need all the data  
> that you want to facet on that doc. One way to do this is the  
> `include_in_parent` option. This takes the value from the children and  
> de-normalizes them into the parent. It's important to note that this means  
> all "relational" data about the field is gone - it is simply a bag of  
> values that exists in the parent now (just like an "inner object"). If you  
> can tolerate that caveat, this will work great for you.

-Zach

On Friday, September 6, 2013 4:12:51 PM UTC-4, J wrote:

> Answer:
> 
> add :include\_in\_parent =\> 'true' for the sub-object, which does not have  
> to be nested and by doing nested : Keywords, all levels will be included.
> 
> On Thursday, September 5, 2013 6:01:45 PM UTC-5, Jessica wrote:
> 
> > I have a very complicated process I'm trying to complete which, to get  
> > the data I need requires the following object format:
> > 
> > Document with values, one value is a nested document. That nested  
> > document itself has a nested document:  
> > general example:  
> > Object do  
> > Keywords do nested object  
> > Name =\> 'string'  
> > Records do nested object  
> > RecName =\> 'string'  
> > RecCount =\> 'integer'  
> > end  
> > end  
> > end
> > 
> > I need to create a terms\_stats facet which will facet on the  
> > Keywords.name value, and will sum the Keywords.Records.RecCount values
> > 
> > I have an example for the documents and my queries/facet formatting at  
> > the following gist: [Elasticsearch Multi-Layered Nested Object: Accessing multiple Nested Layers in a Facet Does not work · GitHub](https://gist.github.com/anonymous/6457316)
> > 
> > In this gist, the last two queries show:
> > 
> > 1. If I use nested:"keywords" then I can correctly facet on the  
> > Keywords.Name value,
> > 2. However it won't let me access the RecCount value and changing the  
> > nested to keywords.records gives no facet results.  
> > \*I've also tried to have multiple nested values with both keywords and  
> > keywords.name, but it seems to only take whichever nested value I give  
> > last.
> > 
> > I really need to be able to access both levels of the documents in the  
> > same facet. I need to have by Name, the total RecCounts and my documents  
> > need to be set up this way for other users.
> > 
> > Please Help!
> > 
> > --J

--  
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:17am UTC](https://discuss.elastic.co/t/multi-leveled-nested-object-facet-search-requiring-multiple-levels-of-nested-object-access-does-not-access-all-fields/13482/4 "2017-07-06T02:17:40Z")

</div>


