Facets on nested objects, plus facet_filter

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:

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.
To view this discussion on the web visit 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.

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 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

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.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit 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.

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<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

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 <javascript:>.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit 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.

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 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 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

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.

To view this discussion on the web visit 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.

--
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.
To view this discussion on the web visit
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.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCXNyUYCLeNGL8BQ8a_RUFGS973ZW72cLf9Pkw0ubma9Q%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.