# Nested documents & faceting

**URL:** <https://discuss.elastic.co/t/nested-documents-faceting/5356>\
**Category:** Elasticsearch\
**Created:** [September 12, 2011, 9:59am UTC](https://discuss.elastic.co/t/nested-documents-faceting/5356 "2011-09-12T09:59:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_2](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Andy\_2](https://discuss.elastic.co/u/Andy_2)\
**Post date:** [September 12, 2011, 9:59am UTC](https://discuss.elastic.co/t/nested-documents-faceting/5356/1 "2011-09-12T09:59:43Z")

</div>

I have a product "Cotton Dress" and 2 items for that product:  
{"size" : "S", "color" : "black"}, {"size" : "M", "color" : "blue"}

1. How do I represent them as a JSON that I can index, is it something  
like:

{  
"dress" {  
"ProductName" : "Cotton Dress",  
"\_id" : 1234567  
"Item" : [  
{  
"size" : "S",  
"color" : "black"  
},  
{  
"size" : "M",  
"color" : "blue"  
}  
]  
}  
}

And what is the mapping for "dress"? I read the doc and is still not  
sure.

1. How does faceting work with a nested document like this? Say I'm  
faceting on "size" and "color",

- the above document should contribute the values "S", "M" for the  
field "size", and the values "black", "blue" for the field "color"
- if a user set a filter on "size" to "S", then the above document  
should only contribute the value "black" (but not "blue") for the  
field "color"  
Is the above true? Or did I misunderstand how nested faceting work?

---

<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:** [September 12, 2011, 3:59pm UTC](https://discuss.elastic.co/t/nested-documents-faceting/5356/2 "2011-09-12T15:59:21Z")

</div>

You need to use nested mapping in this case for item. Explained here:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html),

When faceting, you can facet on nested objects, check here:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/) at the  
bottom.

On Mon, Sep 12, 2011 at 12:59 PM, Andy [selforganized@gmail.com](mailto:selforganized@gmail.com) wrote:

> I have a product "Cotton Dress" and 2 items for that product:  
> {"size" : "S", "color" : "black"}, {"size" : "M", "color" : "blue"}
> 
> 1. How do I represent them as a JSON that I can index, is it something  
> like:
> 
> {  
> "dress" {  
> "ProductName" : "Cotton Dress",  
> "\_id" : 1234567  
> "Item" : [  
> {  
> "size" : "S",  
> "color" : "black"  
> },  
> {  
> "size" : "M",  
> "color" : "blue"  
> }  
> ]  
> }  
> }
> 
> And what is the mapping for "dress"? I read the doc and is still not  
> sure.
> 
> 1. How does faceting work with a nested document like this? Say I'm  
> faceting on "size" and "color",
> 
> - the above document should contribute the values "S", "M" for the  
> field "size", and the values "black", "blue" for the field "color"
> - if a user set a filter on "size" to "S", then the above document  
> should only contribute the value "black" (but not "blue") for the  
> field "color"  
> Is the above true? Or did I misunderstand how nested faceting work?

---

<div class="post-metadata">

**Author:** ![Rauan\_Maemirov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rauan_maemirov/32/826_2.png) [@Rauan\_Maemirov](https://discuss.elastic.co/u/Rauan_Maemirov)\
**Post date:** [June 5, 2012, 3:34pm UTC](https://discuss.elastic.co/t/nested-documents-faceting/5356/3 "2012-06-05T15:34:24Z")

</div>

Hi, Shay.

Could you show here example of nested faceting for Item? (assuming that it  
is declared as nested)

Is it possible to facet on all fields of Item without directly indicating  
it? Smth like  
{"facets": {  
"item\_options":  
{  
"terms": {"field": "Item"},  
"nested": "Item"  
}  
}

I'm still lurking solution for faceting on dynamic attributes.

понедельник, 12 сентября 2011 г., 21:59:21 UTC+6 пользователь kimchy  
написал:

> You need to use nested mapping in this case for item. Explained here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html),
> 
> When faceting, you can facet on nested objects, check here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/) at the  
> bottom.
> 
> On Mon, Sep 12, 2011 at 12:59 PM, Andy \<...\> wrote:
> 
> > I have a product "Cotton Dress" and 2 items for that product:  
> > {"size" : "S", "color" : "black"}, {"size" : "M", "color" : "blue"}
> > 
> > 1. How do I represent them as a JSON that I can index, is it something  
> > like:
> > 
> > {  
> > "dress" {  
> > "ProductName" : "Cotton Dress",  
> > "\_id" : 1234567  
> > "Item" : [  
> > {  
> > "size" : "S",  
> > "color" : "black"  
> > },  
> > {  
> > "size" : "M",  
> > "color" : "blue"  
> > }  
> > ]  
> > }  
> > }
> > 
> > And what is the mapping for "dress"? I read the doc and is still not  
> > sure.
> > 
> > 1. How does faceting work with a nested document like this? Say I'm  
> > faceting on "size" and "color",
> > 
> > - the above document should contribute the values "S", "M" for the  
> > field "size", and the values "black", "blue" for the field "color"
> > - if a user set a filter on "size" to "S", then the above document  
> > should only contribute the value "black" (but not "blue") for the  
> > field "color"  
> > Is the above true? Or did I misunderstand how nested faceting work?

---

<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:25am UTC](https://discuss.elastic.co/t/nested-documents-faceting/5356/4 "2017-07-06T03:25:35Z")

</div>


