# Facets and filtering on nested documents

**URL:** <https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119>\
**Category:** Elasticsearch\
**Created:** [March 12, 2013, 8:12pm UTC](https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119 "2013-03-12T20:12:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Hutchinson](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Simon\_Hutchinson](https://discuss.elastic.co/u/Simon_Hutchinson)\
**Post date:** [March 12, 2013, 8:12pm UTC](https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119/1 "2013-03-12T20:12:12Z")

</div>

Hi All,

I am looking for some advice on the best way to facet on multiple nested  
objects.

The following gist contains some curl requests that will help to illustrate  
what I am doing.

> <https://gist.github.com/sihutch/5146380>

In the final curl command in the above gist I am successfully able to get  
facets from the two nested documents. The response can be seen here:

> <https://gist.github.com/sihutch/5146432>

What I have not been able to work out (if it is possible at all) is how to  
get facets from nested document "contributors" whilst filtering on the  
other nested document "identifiers".

I would appreciate any advice anyone has to offer on:

a) Whether this is possible (From my limited understanding of nested  
documents actually being indexed separately my guess is that it will not  
work)  
b) A different approach that might help me achieve the same goal i.e  
faceted search on multiple nested documents without the problems of "cross  
object" search

Best regards

Si

--  
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 13, 2013, 12:55pm UTC](https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119/2 "2013-03-13T12:55:05Z")

</div>

Hi Simon

> I am looking for some advice on the best way to facet on multiple  
> nested objects.
> 
> The following gist contains some curl requests that will help to  
> illustrate what I am doing.
> 
> [es-nested-facets · GitHub](https://gist.github.com/sihutch/5146380)
> 
> In the final curl command in the above gist I am successfully able to  
> get facets from the two nested documents. The response can be seen  
> here: [es-nest-facets-response · GitHub](https://gist.github.com/sihutch/5146432)
> 
> What I have not been able to work out (if it is possible at all) is  
> how to get facets from nested document "contributors" whilst  
> filtering on the other nested document "identifiers".

I really want to help, but I don't quite follow what you want to do 🙂  
Could you give some example output that you would like to see?

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:** ![Simon\_Hutchinson](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Simon\_Hutchinson](https://discuss.elastic.co/u/Simon_Hutchinson)\
**Post date:** [March 13, 2013, 1:56pm UTC](https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119/3 "2013-03-13T13:56:25Z")

</div>

Hi Clint,

Thanks for replying

> > I really want to help, but I don't quite follow what you want to do 🙂  
> > Could you give some example output that you would like to see?

I shall try to clarify what I want.

If I have a document A that has two "inner objects" B and C each mapped as  
nested is it possible to apply a facet filter for B to a facet of C?

--  
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 14, 2013, 10:30am UTC](https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119/4 "2013-03-14T10:30:09Z")

</div>

Hiya

> If I have a document A that has two "inner objects" B and C each  
> mapped as nested is it possible to apply a facet filter for B to a  
> facet of C?

OK - I think I've got it.

No - you can't filter on one nested document, and facet on the other.  
They are separate documents, and they can't see each other's data.

What you can do (which may or may not suit your requirements) is to set  
"include\_in\_parent" or "include\_in\_root" to true. This indexes the data  
in the nested objects both as nested (ie independent) documents, AND in  
the flattened form that you would have if you had specified type  
"object" instead of type "nested".

so eg a document like:  
{  
one: [{foo: 1}, {foo:2}],  
two: [{bar: 1}, {bar:2}]  
}  
where "one" and "two" are {type: "nested", include\_in\_parent: true}  
would be indexed as:

# parent doc:

{  
one.foo: [1,2],  
two.bar: [1,2]  
}

# plus nested docs:

{ "one.foo": 1}  
{ "one.foo": 2}  
{ "two.bar": 1}  
{ "two.bar": 2}

So then you would be able to facet on values in the parent doc, instead  
of the nested docs. However, on the parent doc, you don't have the  
ability to correlate values within nested docs. Your filter can only  
ask "(do any of the nested docs have value X) and (do any of the nested  
docs have value Y)".

This may or may not suit your needs.

The only other way around it would be to repeat the data within each  
nested doc.

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:** ![Simon\_Hutchinson](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Simon\_Hutchinson](https://discuss.elastic.co/u/Simon_Hutchinson)\
**Post date:** [March 14, 2013, 11:03am UTC](https://discuss.elastic.co/t/facets-and-filtering-on-nested-documents/11119/5 "2013-03-14T11:03:58Z")

</div>

> > If I have a document A that has two "inner objects" B and C each  
> > mapped as nested is it possible to apply a facet filter for B to a  
> > facet of C?
> 
> OK - I think I've got it.
> 
> No - you can't filter on one nested document, and facet on the other.  
> They are separate documents, and they can't see each other's data.
> 
> What you can do (which may or may not suit your requirements) is to set  
> "include\_in\_parent" or "include\_in\_root" to true. This indexes the data  
> in the nested objects both as nested (ie independent) documents, AND in  
> the flattened form that you would have if you had specified type  
> "object" instead of type "nested".
> 
> so eg a document like:  
> {  
> one: [{foo: 1}, {foo:2}],  
> two: [{bar: 1}, {bar:2}]  
> }  
> where "one" and "two" are {type: "nested", include\_in\_parent: true}  
> would be indexed as:
> 
> # parent doc:
> 
> {  
> one.foo: [1,2],  
> two.bar: [1,2]  
> }
> 
> # plus nested docs:
> 
> { "one.foo": 1}  
> { "one.foo": 2}  
> { "two.bar": 1}  
> { "two.bar": 2}
> 
> So then you would be able to facet on values in the parent doc, instead  
> of the nested docs. However, on the parent doc, you don't have the  
> ability to correlate values within nested docs. Your filter can only  
> ask "(do any of the nested docs have value X) and (do any of the nested  
> docs have value Y)".
> 
> This may or may not suit your needs.
> 
> The only other way around it would be to repeat the data within each  
> nested doc.

The behaviour is as I expected but your clarification is much appreciated.  
I have gone with include\_in\_parent: true for now. I will have to consider  
the implications of "cross object" that results from this.

Tips hat graciously to 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:** ![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/facets-and-filtering-on-nested-documents/11119/6 "2017-07-06T02:46:28Z")

</div>


