# Why the special "nested" aggregation and query?

**URL:** <https://discuss.elastic.co/t/why-the-special-nested-aggregation-and-query/17654>\
**Category:** Elasticsearch\
**Created:** [May 21, 2014, 7:55pm UTC](https://discuss.elastic.co/t/why-the-special-nested-aggregation-and-query/17654 "2014-05-21T19:55:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jad/32/1553_2.png) [@Jad](https://discuss.elastic.co/u/Jad)\
**Post date:** [May 21, 2014, 7:55pm UTC](https://discuss.elastic.co/t/why-the-special-nested-aggregation-and-query/17654/1 "2014-05-21T19:55:14Z")

</div>

Hi All,

I've been thinking about why "nested" fields need to be handled with a  
special "nested" query and aggregation type. Is it to handle the case where  
there are multiple nested levels, to be able to control whether a query  
involving two nested fields is within the same nested instance or across  
two nested instances?

Thanks  
Jad.

--  
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/19b47173-41f2-4a5e-9bb8-efc84f0f9a56%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19b47173-41f2-4a5e-9bb8-efc84f0f9a56%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 23, 2014, 12:05am UTC](https://discuss.elastic.co/t/why-the-special-nested-aggregation-and-query/17654/2 "2014-05-23T00:05:08Z")

</div>

Although I would agree that being able to detect it automatically could  
make things simpler, I think that the fact that it is excplicit is more  
flexible. For example, it can make sense to copy field values into the root  
document[1]. This can help speed-up some queries that don't need to know  
about the tree structure of your document. And in that case you have two  
ways to search the same field name:

- either through the root document: faster but less flexible
- or through the nested document: less flexible but slower

The fact that nested queries are explicit allows you to choose the way that  
you want the field to be queried.

For aggregations, I think it is also nice to make it explicit so that  
counts are not surprising: imagine that you have a document with properties  
stored as nested documents and each property having a name. If you run a  
terms aggregation on the property name from the root document, buckets will  
count how many root documents have this property name. On the other hand,  
if you run this terms aggregation through a nested field, this will count  
the number of _properties_ that have this name. Since each document can  
have several properties, counts might be much higher.

[1]

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Wed, May 21, 2014 at 9:55 PM, Jad [jnaous@gmail.com](mailto:jnaous@gmail.com) wrote:

> Hi All,
> 
> I've been thinking about why "nested" fields need to be handled with a  
> special "nested" query and aggregation type. Is it to handle the case where  
> there are multiple nested levels, to be able to control whether a query  
> involving two nested fields is within the same nested instance or across  
> two nested instances?
> 
> Thanks  
> Jad.
> 
> --  
> 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/19b47173-41f2-4a5e-9bb8-efc84f0f9a56%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19b47173-41f2-4a5e-9bb8-efc84f0f9a56%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/19b47173-41f2-4a5e-9bb8-efc84f0f9a56%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/19b47173-41f2-4a5e-9bb8-efc84f0f9a56%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j60svm9864bHyz1Da0JU%3DLN3%3DtgOCoyPN06rN5Q0oE4NA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j60svm9864bHyz1Da0JU%3DLN3%3DtgOCoyPN06rN5Q0oE4NA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:27am UTC](https://discuss.elastic.co/t/why-the-special-nested-aggregation-and-query/17654/3 "2017-07-06T01:27:32Z")

</div>


