# Nested objects and facets

**URL:** <https://discuss.elastic.co/t/nested-objects-and-facets/11156>\
**Category:** Elasticsearch\
**Created:** [March 14, 2013, 1:14pm UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156 "2013-03-14T13:14:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_Mlynarczyk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawel_mlynarczyk/32/1568_2.png) [@Pawel\_Mlynarczyk](https://discuss.elastic.co/u/Pawel_Mlynarczyk)\
**Post date:** [March 14, 2013, 1:14pm UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/1 "2013-03-14T13:14:05Z")

</div>

Hello everybody

I'm working on a facet navigation and I've encountered an issue.

Let me explain what I'm trying to achieve first. Take a look at  
[http://www.pricerunner.co.uk/cl/2/TVs](http://www.pricerunner.co.uk/cl/2/TVs) (not my site, just a basic example).  
When you chose one filter, then it's applied to all facets but that  
particular one (it's logical, because TVs cant be 30'' and 32'' at the same  
time). That's simply achieved, but the problem arises when we want to add a  
facet that is based on a field in a nested structure.

Let's say we've got a structure (mapping) like this:

curl -XPUT 'localhost:9200/items' -d '  
{  
"mappings" : {  
"Item" : {  
"dynamic" : false,  
"properties" : {  
"id" : { "type" : "string", "index" : "not\_analyzed" },  
"name" : { "type" : "string", "index" : "not\_analyzed" },  
"price" : { "type" : "double", "index" : "not\_analyzed" },  
"type" : { "type" : "string", "index" : "not\_analyzed" },  
"characteristics" : {  
"type" : "nested",  
"properties" : {  
"name" : { "type" : "string", "index" : "not\_analyzed" },  
"value" : { "type" : "string", "index" : "not\_analyzed" }  
}  
}  
}  
}  
}  
}  
'

We want facets for the fields in the parent object - that's easy - and a  
facet for characteristics (name -\> value) - and thats troublesome.

So having the above mapping, my app would generate a query like this (let's  
assume that some restrictions are already made):

curl -X POST 'localhost:9200/items/\_search?pretty=true' -d '  
{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"screen size" : {  
"nested" : "characteristics",  
"terms" : { "field" : "characteristics.value" },  
"facet\_filter" : {  
"and" : [  
{ "term" : { "characteristics.name" : "screen size" } }  
]  
}  
},  
"price" : {  
"terms" : { "field" : "price" },  
"facet\_filter" : {  
"and" : [  
{ "term" : { "type" : "TV" } },  
{ "term" : { "name" : "Shark largedisplay" } }  
]  
}  
},  
"type" : {  
"terms" : { "field" : "type" },  
"facet\_filter" : {  
"and" : [  
{ "term" : { "name" : "Shark largedisplay" } }  
]  
}  
},  
"name" : {  
"terms" : { "field" : "name" },  
"facet\_filter" : {  
"and" : [  
{ "term" : { "type" : "TV" } }  
]  
}  
}  
}  
}  
'

As you can see, I am able to provide some restrictions for regular facets.  
Filter for the facet includes only conditions that are NOT related to the  
field the facet is based on. But I'm not able to do this for the nested  
facet, because I can't target fields of the parent objects from there.

I am curious about the 'proper' approach - this in my opinion should be a  
common usage for the facets.  
I would appriciate any given help.

Thanks in advance.  
Paweł

--  
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:** ![Dustin\_Lashmar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dustin_lashmar/32/2594_2.png) [@Dustin\_Lashmar](https://discuss.elastic.co/u/Dustin_Lashmar)\
**Post date:** [March 15, 2013, 12:53am UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/2 "2013-03-15T00:53:27Z")

</div>

Hi,

I've tried to achieve something similar, without much luck, (and I don't  
think we're the only ones).  
What's the purpose of having the characteristics as a name/value object?  
Perhaps you could try indexing these characteristics in the parent doc as  
regular fields, (so "screen size" is the field name, not the value of the  
"name" field) and just updating the mappings if a new one comes along, that  
way you could perform the facets over them as you would normally.  
If you need to work with mappings like this then I'm afraid it's not  
possible yet (at least not according to:  
[https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/oDcyUNU1cW8](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/oDcyUNU1cW8)  
and a few others), but it seems to be a popular topic lately, so hopefully  
there will be some development around this sometime soon,

Dustin

On Friday, March 15, 2013 12:14:05 AM UTC+11, Paweł Młynarczyk wrote:

> Hello everybody
> 
> I'm working on a facet navigation and I've encountered an issue.
> 
> Let me explain what I'm trying to achieve first. Take a look at  
> [TVs (1000+ products) compare today & see the best prices »](http://www.pricerunner.co.uk/cl/2/TVs) (not my site, just a basic  
> example). When you chose one filter, then it's applied to all facets but  
> that particular one (it's logical, because TVs cant be 30'' and 32'' at the  
> same time). That's simply achieved, but the problem arises when we want to  
> add a facet that is based on a field in a nested structure.
> 
> Let's say we've got a structure (mapping) like this:
> 
> curl -XPUT 'localhost:9200/items' -d '  
> {  
> "mappings" : {  
> "Item" : {  
> "dynamic" : false,  
> "properties" : {  
> "id" : { "type" : "string", "index" : "not\_analyzed" },  
> "name" : { "type" : "string", "index" : "not\_analyzed" },  
> "price" : { "type" : "double", "index" : "not\_analyzed" },  
> "type" : { "type" : "string", "index" : "not\_analyzed" },  
> "characteristics" : {  
> "type" : "nested",  
> "properties" : {  
> "name" : { "type" : "string", "index" : "not\_analyzed" },  
> "value" : { "type" : "string", "index" : "not\_analyzed" }  
> }  
> }  
> }  
> }  
> }  
> }  
> '
> 
> We want facets for the fields in the parent object - that's easy - and a  
> facet for characteristics (name -\> value) - and thats troublesome.
> 
> So having the above mapping, my app would generate a query like this  
> (let's assume that some restrictions are already made):
> 
> curl -X POST 'localhost:9200/items/\_search?pretty=true' -d '  
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "facets" : {  
> "screen size" : {  
> "nested" : "characteristics",  
> "terms" : { "field" : "characteristics.value" },  
> "facet\_filter" : {  
> "and" : [  
> { "term" : { "characteristics.name" : "screen size" } }  
> ]  
> }  
> },  
> "price" : {  
> "terms" : { "field" : "price" },  
> "facet\_filter" : {  
> "and" : [  
> { "term" : { "type" : "TV" } },  
> { "term" : { "name" : "Shark largedisplay" } }  
> ]  
> }  
> },  
> "type" : {  
> "terms" : { "field" : "type" },  
> "facet\_filter" : {  
> "and" : [  
> { "term" : { "name" : "Shark largedisplay" } }  
> ]  
> }  
> },  
> "name" : {  
> "terms" : { "field" : "name" },  
> "facet\_filter" : {  
> "and" : [  
> { "term" : { "type" : "TV" } }  
> ]  
> }  
> }  
> }  
> }  
> '
> 
> As you can see, I am able to provide some restrictions for regular facets.  
> Filter for the facet includes only conditions that are NOT related to the  
> field the facet is based on. But I'm not able to do this for the nested  
> facet, because I can't target fields of the parent objects from there.
> 
> I am curious about the 'proper' approach - this in my opinion should be a  
> common usage for the facets.  
> I would appriciate any given help.
> 
> Thanks in advance.  
> Paweł

--  
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:** ![Pawel\_Mlynarczyk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawel_mlynarczyk/32/1568_2.png) [@Pawel\_Mlynarczyk](https://discuss.elastic.co/u/Pawel_Mlynarczyk)\
**Post date:** [March 15, 2013, 9:05am UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/3 "2013-03-15T09:05:50Z")

</div>

Hi Dustin, thanks for your reply.

Indexing those characteristics as a regular fields would lead to a very  
huge Item mapping, since it has to contain every characteristic. In  
addition it would have to be a dynamic mapping since I don't have the  
characteristics in advance.

W dniu piątek, 15 marca 2013 01:53:27 UTC+1 użytkownik Dustin Lashmar  
napisał:

> Hi,
> 
> I've tried to achieve something similar, without much luck, (and I don't  
> think we're the only ones).  
> What's the purpose of having the characteristics as a name/value object?  
> Perhaps you could try indexing these characteristics in the parent doc as  
> regular fields, (so "screen size" is the field name, not the value of the  
> "name" field) and just updating the mappings if a new one comes along, that  
> way you could perform the facets over them as you would normally.  
> If you need to work with mappings like this then I'm afraid it's not  
> possible yet (at least not according to:  
> [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/oDcyUNU1cW8and) a few others), but it seems to be a popular topic lately, so hopefully  
> there will be some development around this sometime soon,
> 
> Dustin
> 
> On Friday, March 15, 2013 12:14:05 AM UTC+11, Paweł Młynarczyk wrote:
> 
> > Hello everybody
> > 
> > I'm working on a facet navigation and I've encountered an issue.
> > 
> > Let me explain what I'm trying to achieve first. Take a look at  
> > [TVs (1000+ products) compare today & see the best prices »](http://www.pricerunner.co.uk/cl/2/TVs) (not my site, just a basic  
> > example). When you chose one filter, then it's applied to all facets but  
> > that particular one (it's logical, because TVs cant be 30'' and 32'' at the  
> > same time). That's simply achieved, but the problem arises when we want to  
> > add a facet that is based on a field in a nested structure.
> > 
> > Let's say we've got a structure (mapping) like this:
> > 
> > curl -XPUT 'localhost:9200/items' -d '  
> > {  
> > "mappings" : {  
> > "Item" : {  
> > "dynamic" : false,  
> > "properties" : {  
> > "id" : { "type" : "string", "index" : "not\_analyzed" },  
> > "name" : { "type" : "string", "index" : "not\_analyzed" },  
> > "price" : { "type" : "double", "index" : "not\_analyzed" },  
> > "type" : { "type" : "string", "index" : "not\_analyzed" },  
> > "characteristics" : {  
> > "type" : "nested",  
> > "properties" : {  
> > "name" : { "type" : "string", "index" : "not\_analyzed" },  
> > "value" : { "type" : "string", "index" : "not\_analyzed" }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > '
> > 
> > We want facets for the fields in the parent object - that's easy - and a  
> > facet for characteristics (name -\> value) - and thats troublesome.
> > 
> > So having the above mapping, my app would generate a query like this  
> > (let's assume that some restrictions are already made):
> > 
> > curl -X POST 'localhost:9200/items/\_search?pretty=true' -d '  
> > {  
> > "query" : {  
> > "match\_all" : {}  
> > },  
> > "facets" : {  
> > "screen size" : {  
> > "nested" : "characteristics",  
> > "terms" : { "field" : "characteristics.value" },  
> > "facet\_filter" : {  
> > "and" : [  
> > { "term" : { "characteristics.name" : "screen size" } }  
> > ]  
> > }  
> > },  
> > "price" : {  
> > "terms" : { "field" : "price" },  
> > "facet\_filter" : {  
> > "and" : [  
> > { "term" : { "type" : "TV" } },  
> > { "term" : { "name" : "Shark largedisplay" } }  
> > ]  
> > }  
> > },  
> > "type" : {  
> > "terms" : { "field" : "type" },  
> > "facet\_filter" : {  
> > "and" : [  
> > { "term" : { "name" : "Shark largedisplay" } }  
> > ]  
> > }  
> > },  
> > "name" : {  
> > "terms" : { "field" : "name" },  
> > "facet\_filter" : {  
> > "and" : [  
> > { "term" : { "type" : "TV" } }  
> > ]  
> > }  
> > }  
> > }  
> > }  
> > '
> > 
> > As you can see, I am able to provide some restrictions for regular  
> > facets. Filter for the facet includes only conditions that are NOT related  
> > to the field the facet is based on. But I'm not able to do this for the  
> > nested facet, because I can't target fields of the parent objects from  
> > there.
> > 
> > I am curious about the 'proper' approach - this in my opinion should be a  
> > common usage for the facets.  
> > I would appriciate any given help.
> > 
> > Thanks in advance.  
> > Paweł

--  
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:** ![Dustin\_Lashmar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dustin_lashmar/32/2594_2.png) [@Dustin\_Lashmar](https://discuss.elastic.co/u/Dustin_Lashmar)\
**Post date:** [March 18, 2013, 4:20am UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/4 "2013-03-18T04:20:23Z")

</div>

Ok fair enough, sorry I couldn't help more. Maybe someone else will be able  
to help.

On Friday, March 15, 2013 8:05:50 PM UTC+11, Paweł Młynarczyk wrote:

> Hi Dustin, thanks for your reply.
> 
> Indexing those characteristics as a regular fields would lead to a very  
> huge Item mapping, since it has to contain every characteristic. In  
> addition it would have to be a dynamic mapping since I don't have the  
> characteristics in advance.
> 
> W dniu piątek, 15 marca 2013 01:53:27 UTC+1 użytkownik Dustin Lashmar  
> napisał:
> 
> > Hi,
> > 
> > I've tried to achieve something similar, without much luck, (and I don't  
> > think we're the only ones).  
> > What's the purpose of having the characteristics as a name/value object?  
> > Perhaps you could try indexing these characteristics in the parent doc as  
> > regular fields, (so "screen size" is the field name, not the value of the  
> > "name" field) and just updating the mappings if a new one comes along, that  
> > way you could perform the facets over them as you would normally.  
> > If you need to work with mappings like this then I'm afraid it's not  
> > possible yet (at least not according to:  
> > [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/oDcyUNU1cW8and) a few others), but it seems to be a popular topic lately, so hopefully  
> > there will be some development around this sometime soon,
> > 
> > Dustin
> > 
> > On Friday, March 15, 2013 12:14:05 AM UTC+11, Paweł Młynarczyk wrote:
> > 
> > > Hello everybody
> > > 
> > > I'm working on a facet navigation and I've encountered an issue.
> > > 
> > > Let me explain what I'm trying to achieve first. Take a look at  
> > > [TVs (1000+ products) compare today & see the best prices »](http://www.pricerunner.co.uk/cl/2/TVs) (not my site, just a basic  
> > > example). When you chose one filter, then it's applied to all facets but  
> > > that particular one (it's logical, because TVs cant be 30'' and 32'' at the  
> > > same time). That's simply achieved, but the problem arises when we want to  
> > > add a facet that is based on a field in a nested structure.
> > > 
> > > Let's say we've got a structure (mapping) like this:
> > > 
> > > curl -XPUT 'localhost:9200/items' -d '  
> > > {  
> > > "mappings" : {  
> > > "Item" : {  
> > > "dynamic" : false,  
> > > "properties" : {  
> > > "id" : { "type" : "string", "index" : "not\_analyzed" },  
> > > "name" : { "type" : "string", "index" : "not\_analyzed" },  
> > > "price" : { "type" : "double", "index" : "not\_analyzed" },  
> > > "type" : { "type" : "string", "index" : "not\_analyzed" },  
> > > "characteristics" : {  
> > > "type" : "nested",  
> > > "properties" : {  
> > > "name" : { "type" : "string", "index" : "not\_analyzed" },  
> > > "value" : { "type" : "string", "index" : "not\_analyzed" }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > '
> > > 
> > > We want facets for the fields in the parent object - that's easy - and a  
> > > facet for characteristics (name -\> value) - and thats troublesome.
> > > 
> > > So having the above mapping, my app would generate a query like this  
> > > (let's assume that some restrictions are already made):
> > > 
> > > curl -X POST 'localhost:9200/items/\_search?pretty=true' -d '  
> > > {  
> > > "query" : {  
> > > "match\_all" : {}  
> > > },  
> > > "facets" : {  
> > > "screen size" : {  
> > > "nested" : "characteristics",  
> > > "terms" : { "field" : "characteristics.value" },  
> > > "facet\_filter" : {  
> > > "and" : [  
> > > { "term" : { "characteristics.name" : "screen size" } }  
> > > ]  
> > > }  
> > > },  
> > > "price" : {  
> > > "terms" : { "field" : "price" },  
> > > "facet\_filter" : {  
> > > "and" : [  
> > > { "term" : { "type" : "TV" } },  
> > > { "term" : { "name" : "Shark largedisplay" } }  
> > > ]  
> > > }  
> > > },  
> > > "type" : {  
> > > "terms" : { "field" : "type" },  
> > > "facet\_filter" : {  
> > > "and" : [  
> > > { "term" : { "name" : "Shark largedisplay" } }  
> > > ]  
> > > }  
> > > },  
> > > "name" : {  
> > > "terms" : { "field" : "name" },  
> > > "facet\_filter" : {  
> > > "and" : [  
> > > { "term" : { "type" : "TV" } }  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > }  
> > > '
> > > 
> > > As you can see, I am able to provide some restrictions for regular  
> > > facets. Filter for the facet includes only conditions that are NOT related  
> > > to the field the facet is based on. But I'm not able to do this for the  
> > > nested facet, because I can't target fields of the parent objects from  
> > > there.
> > > 
> > > I am curious about the 'proper' approach - this in my opinion should be  
> > > a common usage for the facets.  
> > > I would appriciate any given help.
> > > 
> > > Thanks in advance.  
> > > Paweł

--  
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:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 18, 2013, 9:26am UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/5 "2013-03-18T09:26:26Z")

</div>

Hi Paweł

> Indexing those characteristics as a regular fields would lead to a  
> very huge Item mapping, since it has to contain every characteristic.  
> In addition it would have to be a dynamic mapping since I don't have  
> the characteristics in advance.

Yes, this is indeed a problem. Currently you can't use a filter facet  
that is applied both to the parent doc and to the nested doc.

This is something that we hope to solve in a big refactoring of facets,  
which will make them much more flexible.

however, in the meantime, I suggest you also index a field in the parent  
whose value is something like: "key\_value", eg "tvsize\_32"

That field can be an array containing multiple values, one for each  
characteristic.

That way, you can filter on that value, without having your mapping  
explode

clint

--  
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:** ![Pawel\_Mlynarczyk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawel_mlynarczyk/32/1568_2.png) [@Pawel\_Mlynarczyk](https://discuss.elastic.co/u/Pawel_Mlynarczyk)\
**Post date:** [March 18, 2013, 3:45pm UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/6 "2013-03-18T15:45:49Z")

</div>

Hi Clint, thanks for your reply.

> however, in the meantime, I suggest you also index a field in the parent  
> whose value is something like: "key\_value", eg "tvsize\_32"

Do you mean something like this: [Mappings for elasticsearch not nested stuff · GitHub](https://gist.github.com/zwrss/5188008) ?

Paweł

--  
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:46am UTC](https://discuss.elastic.co/t/nested-objects-and-facets/11156/7 "2017-07-06T02:46:04Z")

</div>


