# Facets on nested objects, plus facet\_filter

**URL:** https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087
**Category:** Elasticsearch
**Created:** [January 2, 2014, 10:46pm UTC](https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087 "2014-01-02T22:46:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nathan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_2/32/1875_2.png) [@Nathan\_2](https://discuss.elastic.co/u/Nathan_2)
#### Post date: [January 2, 2014, 10:46pm UTC](https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087/1 "2014-01-02T22:46:03Z")

</div>

Hi, I am using nested objects for indexing “ratings” on an object, where a rating contains two properties: the owner and the rating. I want to be able to filter and facet on “my ratings”. So to filter, for example, on objects I have rated a “10", I am using a filter like

{  
“nested” : {  
“path” : “ratings”,  
“filter” : {  
“and” : [{  
“term” { “ratings.rating” : 10 },  
“term” { “ratings.owner” : “my\_id” }  
}]  
}  
}  
}

I also want to facet on “my rating”, which in a basic form I’m doing like this:

“facets” : {  
“my\_ratings” : {  
“nested” : “ratings”,  
“terms” : {  
“field” : “ratings.rating”,  
“size” : 10  
},  
“facet\_filter” : {  
“term” : { “ratings.owner” : “my\_id” }  
}  
}  
}

That seems to be working fine. The problem is when I have other filters in the mix. If I am also filtering my query by other fields, I need to include those filters in my facet, so that I’m getting back facet counts that match the results with the other filters applied. My problem is that I don’t know how to combine nested and non-nested filters in facet\_filter. If I just throw them in together, my counts all go to zero:

“facets” : {  
“my\_ratings” : {  
“nested” : “ratings”,  
“terms” : {  
“field” : “ratings.rating”,  
“size” : 10  
},  
“facet\_filter” : {  
“and” : [{  
“term” : { “ratings.owner” : “my\_id” }  
},{  
“term” : { “a\_different\_field” : “blah” }  
}]  
}  
}  
}

Here is a gist to demonstrate:

> <https://gist.github.com/nathanmoon/8228507>

It runs two queries, the first is the basic nested facet (returns what I would expect), and the last query is what I want to get working, but is returning no counts.

Thanks for any help!

Nathan

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%40gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [January 2, 2014, 10:54pm UTC](https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087/2 "2014-01-02T22:54:30Z")

</div>

AFAIK, you cannot filter on parent fields when faceting on nested documents.

Cheers,

Ivan

On Thu, Jan 2, 2014 at 2:46 PM, Nathan Moon [nathannospam@gmail.com](mailto:nathannospam@gmail.com) wrote:

> Hi, I am using nested objects for indexing “ratings” on an object, where a  
> rating contains two properties: the owner and the rating. I want to be  
> able to filter and facet on “my ratings”. So to filter, for example, on  
> objects I have rated a “10", I am using a filter like
> 
> {  
> “nested” : {  
> “path” : “ratings”,  
> “filter” : {  
> “and” : [{  
> “term” { “ratings.rating” : 10 },  
> “term” { “ratings.owner” : “my\_id” }  
> }]  
> }  
> }  
> }
> 
> I also want to facet on “my rating”, which in a basic form I’m doing like  
> this:
> 
> “facets” : {  
> “my\_ratings” : {  
> “nested” : “ratings”,  
> “terms” : {  
> “field” : “ratings.rating”,  
> “size” : 10  
> },  
> “facet\_filter” : {  
> “term” : { “ratings.owner” : “my\_id” }  
> }  
> }  
> }
> 
> That seems to be working fine. The problem is when I have other filters in  
> the mix. If I am also filtering my query by other fields, I need to  
> include those filters in my facet, so that I’m getting back facet counts  
> that match the results with the other filters applied. My problem is that  
> I don’t know how to combine nested and non-nested filters in facet\_filter.  
> If I just throw them in together, my counts all go to zero:
> 
> “facets” : {  
> “my\_ratings” : {  
> “nested” : “ratings”,  
> “terms” : {  
> “field” : “ratings.rating”,  
> “size” : 10  
> },  
> “facet\_filter” : {  
> “and” : [{  
> “term” : { “ratings.owner” : “my\_id” }  
> },{  
> “term” : { “a\_different\_field” : “blah” }  
> }]  
> }  
> }  
> }
> 
> Here is a gist to demonstrate:
> 
> [elasticsearch nested facets and facet\_filters · GitHub](https://gist.github.com/nathanmoon/8228507)
> 
> It runs two queries, the first is the basic nested facet (returns what I  
> would expect), and the last query is what I want to get working, but is  
> returning no counts.
> 
> Thanks for any help!
> 
> Nathan
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%40gmail.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCX\_Sdna\_A9LAOWtrGT4wHg1OFa2bJW\_je-iDWEXgqRCQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCX_Sdna_A9LAOWtrGT4wHg1OFa2bJW_je-iDWEXgqRCQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Nathan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_2/32/1875_2.png) [@Nathan\_2](https://discuss.elastic.co/u/Nathan_2)
#### Post date: [January 3, 2014, 4:15pm UTC](https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087/3 "2014-01-03T16:15:10Z")

</div>

That is unfortunate. When filtering/faceting by multiple fields, that is  
pretty standard behavior to facet\_filter the facets, I wonder what others  
are doing to work around this? I looked to see if there was a ticket  
created to cover this, and didn't find one. Can others who are more  
familiar than me with the current state of elasticsearch weigh in on this?  
Is this something I should open a new bug fix/feature request for? Or is  
there some other way of tackling this problem?

Thanks!

Nathan

On Thursday, January 2, 2014 3:54:30 PM UTC-7, Ivan Brusic wrote:

> AFAIK, you cannot filter on parent fields when faceting on nested  
> documents.
> 
> Cheers,
> 
> Ivan
> 
> On Thu, Jan 2, 2014 at 2:46 PM, Nathan Moon \<[nathan...@gmail.com](mailto:nathan...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi, I am using nested objects for indexing “ratings” on an object, where  
> > a rating contains two properties: the owner and the rating. I want to be  
> > able to filter and facet on “my ratings”. So to filter, for example, on  
> > objects I have rated a “10", I am using a filter like
> > 
> > {  
> > “nested” : {  
> > “path” : “ratings”,  
> > “filter” : {  
> > “and” : [{  
> > “term” { “ratings.rating” : 10 },  
> > “term” { “ratings.owner” : “my\_id” }  
> > }]  
> > }  
> > }  
> > }
> > 
> > I also want to facet on “my rating”, which in a basic form I’m doing like  
> > this:
> > 
> > “facets” : {  
> > “my\_ratings” : {  
> > “nested” : “ratings”,  
> > “terms” : {  
> > “field” : “ratings.rating”,  
> > “size” : 10  
> > },  
> > “facet\_filter” : {  
> > “term” : { “ratings.owner” : “my\_id” }  
> > }  
> > }  
> > }
> > 
> > That seems to be working fine. The problem is when I have other filters  
> > in the mix. If I am also filtering my query by other fields, I need to  
> > include those filters in my facet, so that I’m getting back facet counts  
> > that match the results with the other filters applied. My problem is that  
> > I don’t know how to combine nested and non-nested filters in facet\_filter.  
> > If I just throw them in together, my counts all go to zero:
> > 
> > “facets” : {  
> > “my\_ratings” : {  
> > “nested” : “ratings”,  
> > “terms” : {  
> > “field” : “ratings.rating”,  
> > “size” : 10  
> > },  
> > “facet\_filter” : {  
> > “and” : [{  
> > “term” : { “ratings.owner” : “my\_id” }  
> > },{  
> > “term” : { “a\_different\_field” : “blah” }  
> > }]  
> > }  
> > }  
> > }
> > 
> > Here is a gist to demonstrate:
> > 
> > [elasticsearch nested facets and facet\_filters · GitHub](https://gist.github.com/nathanmoon/8228507)
> > 
> > It runs two queries, the first is the basic nested facet (returns what I  
> > would expect), and the last query is what I want to get working, but is  
> > returning no counts.
> > 
> > Thanks for any help!
> > 
> > Nathan
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%40gmail.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3d0e4a1e-66c8-4646-9862-06404ba6f9b3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3d0e4a1e-66c8-4646-9862-06404ba6f9b3%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [January 3, 2014, 5:18pm UTC](https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087/4 "2014-01-03T17:18:00Z")

</div>

Underneath, elasticsearch uses Lucene's ToParentBlockJoinQuery, so the  
limitation must be somewhere there. I have never used join queries directly  
in Lucene, since most of the Lucene code I wrote was in Lucene \<  
3.5. Martijn van Groningen is the expert, so hopefully he will chime in.

--  
Ivan

On Fri, Jan 3, 2014 at 8:15 AM, Nathan [nathannospam@gmail.com](mailto:nathannospam@gmail.com) wrote:

> That is unfortunate. When filtering/faceting by multiple fields, that is  
> pretty standard behavior to facet\_filter the facets, I wonder what others  
> are doing to work around this? I looked to see if there was a ticket  
> created to cover this, and didn't find one. Can others who are more  
> familiar than me with the current state of elasticsearch weigh in on this?  
> Is this something I should open a new bug fix/feature request for? Or is  
> there some other way of tackling this problem?
> 
> Thanks!
> 
> Nathan
> 
> On Thursday, January 2, 2014 3:54:30 PM UTC-7, Ivan Brusic wrote:
> 
> > AFAIK, you cannot filter on parent fields when faceting on nested  
> > documents.
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Thu, Jan 2, 2014 at 2:46 PM, Nathan Moon [nathan...@gmail.com](mailto:nathan...@gmail.com) wrote:
> > 
> > > Hi, I am using nested objects for indexing “ratings” on an object, where  
> > > a rating contains two properties: the owner and the rating. I want to be  
> > > able to filter and facet on “my ratings”. So to filter, for example, on  
> > > objects I have rated a “10", I am using a filter like
> > > 
> > > {  
> > > “nested” : {  
> > > “path” : “ratings”,  
> > > “filter” : {  
> > > “and” : [{  
> > > “term” { “ratings.rating” : 10 },  
> > > “term” { “ratings.owner” : “my\_id” }  
> > > }]  
> > > }  
> > > }  
> > > }
> > > 
> > > I also want to facet on “my rating”, which in a basic form I’m doing  
> > > like this:
> > > 
> > > “facets” : {  
> > > “my\_ratings” : {  
> > > “nested” : “ratings”,  
> > > “terms” : {  
> > > “field” : “ratings.rating”,  
> > > “size” : 10  
> > > },  
> > > “facet\_filter” : {  
> > > “term” : { “ratings.owner” : “my\_id” }  
> > > }  
> > > }  
> > > }
> > > 
> > > That seems to be working fine. The problem is when I have other filters  
> > > in the mix. If I am also filtering my query by other fields, I need to  
> > > include those filters in my facet, so that I’m getting back facet counts  
> > > that match the results with the other filters applied. My problem is that  
> > > I don’t know how to combine nested and non-nested filters in facet\_filter.  
> > > If I just throw them in together, my counts all go to zero:
> > > 
> > > “facets” : {  
> > > “my\_ratings” : {  
> > > “nested” : “ratings”,  
> > > “terms” : {  
> > > “field” : “ratings.rating”,  
> > > “size” : 10  
> > > },  
> > > “facet\_filter” : {  
> > > “and” : [{  
> > > “term” : { “ratings.owner” : “my\_id” }  
> > > },{  
> > > “term” : { “a\_different\_field” : “blah” }  
> > > }]  
> > > }  
> > > }  
> > > }
> > > 
> > > Here is a gist to demonstrate:
> > > 
> > > [elasticsearch nested facets and facet\_filters · GitHub](https://gist.github.com/nathanmoon/8228507)
> > > 
> > > It runs two queries, the first is the basic nested facet (returns what I  
> > > would expect), and the last query is what I want to get working, but is  
> > > returning no counts.
> > > 
> > > Thanks for any help!
> > > 
> > > Nathan
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/A3B6ECDB-9CB5-4532-A2F9-8EAA66B9EFD0%[40gmail.com](http://40gmail.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/3d0e4a1e-66c8-4646-9862-06404ba6f9b3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3d0e4a1e-66c8-4646-9862-06404ba6f9b3%40googlegroups.com)  
> > .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCXNyUYCLeNGL8BQ8a\_RUFGS973ZW72cLf9Pkw0ubma9Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCXNyUYCLeNGL8BQ8a_RUFGS973ZW72cLf9Pkw0ubma9Q%40mail.gmail.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, 1:58am UTC](https://discuss.elastic.co/t/facets-on-nested-objects-plus-facet-filter/15087/5 "2017-07-06T01:58:27Z")

</div>


