# Filter in nested aggregations

**URL:** <https://discuss.elastic.co/t/filter-in-nested-aggregations/21632>\
**Category:** Elasticsearch\
**Created:** [January 14, 2015, 10:08am UTC](https://discuss.elastic.co/t/filter-in-nested-aggregations/21632 "2015-01-14T10:08:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loic\_Wenkin](https://avatars.discourse-cdn.com/v4/letter/l/4491bb/32.png) [@Loic\_Wenkin](https://discuss.elastic.co/u/Loic_Wenkin)\
**Post date:** [January 14, 2015, 10:08am UTC](https://discuss.elastic.co/t/filter-in-nested-aggregations/21632/1 "2015-01-14T10:08:34Z")

</div>

Hi all,

I was wondering if it is possible to apply aggregations only on a set of  
nested objects (not all nested objects presents inside a document),  
according to a property of this object? I think an example will help to  
understand.

Let's say that you have the following documents inthe index:

{  
"a": 2,  
"b": 3,  
"c": 4,  
"d": [  
{  
"toto": 1,  
"tutu": 2,  
"titi": 3  
},  
{  
"toto": 2,  
"tutu": 5,  
"titi": 6  
},  
{  
"toto": 3,  
"tutu": 11,  
"titi": 8  
},  
{  
"toto": 4,  
"tutu": 7,  
"titi": 4  
}  
]  
}

{  
"a": 3,  
"b": 4,  
"c": 5,  
"d": [  
{  
"toto": 1,  
"tutu": 10,  
"titi": 6  
},  
{  
"toto": 2,  
"tutu": 65,  
"titi": 8  
},  
{  
"toto": 3,  
"tutu": 25,  
"titi": 15  
},  
{  
"toto": 4,  
"tutu": 30,  
"titi": 45  
}  
]  
}

Where "d" is indexed as nested. I would like to get the maximum value for  
the "tutu" property from this kind of documents, but I would like to work  
only on nested objects where "toto" = 1. Here, I would like to get 10 (the  
red one in the samples), and not 65.

Is it possible to do this kind of aggregation? Or have I to change my  
document structure (or index mapping or ...)? If it is possible, could you  
point me how to do it? I had a look at nested aggregations and filter ones,  
but I am not sure that it could help.

Thank you for your replies.

Regards,  
Loï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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%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:** [January 14, 2015, 11:05am UTC](https://discuss.elastic.co/t/filter-in-nested-aggregations/21632/2 "2015-01-14T11:05:58Z")

</div>

Provided that `d` is mapped as a `nested` object, you can do it by using  
the nested aggregation. Here are the aggregations that you would need on  
each level

1. nested aggregation to be in the context of path='d'
2. filter aggregation on d.toto == 1
3. max aggregation on d.tutu

On Wed, Jan 14, 2015 at 11:08 AM, Loïc Wenkin [loic.wenkin@gmail.com](mailto:loic.wenkin@gmail.com) wrote:

> Hi all,
> 
> I was wondering if it is possible to apply aggregations only on a set of  
> nested objects (not all nested objects presents inside a document),  
> according to a property of this object? I think an example will help to  
> understand.
> 
> Let's say that you have the following documents inthe index:
> 
> {  
> "a": 2,  
> "b": 3,  
> "c": 4,  
> "d": [  
> {  
> "toto": 1,  
> "tutu": 2,  
> "titi": 3  
> },  
> {  
> "toto": 2,  
> "tutu": 5,  
> "titi": 6  
> },  
> {  
> "toto": 3,  
> "tutu": 11,  
> "titi": 8  
> },  
> {  
> "toto": 4,  
> "tutu": 7,  
> "titi": 4  
> }  
> ]  
> }
> 
> {  
> "a": 3,  
> "b": 4,  
> "c": 5,  
> "d": [  
> {  
> "toto": 1,  
> "tutu": 10,  
> "titi": 6  
> },  
> {  
> "toto": 2,  
> "tutu": 65,  
> "titi": 8  
> },  
> {  
> "toto": 3,  
> "tutu": 25,  
> "titi": 15  
> },  
> {  
> "toto": 4,  
> "tutu": 30,  
> "titi": 45  
> }  
> ]  
> }
> 
> Where "d" is indexed as nested. I would like to get the maximum value for  
> the "tutu" property from this kind of documents, but I would like to work  
> only on nested objects where "toto" = 1. Here, I would like to get 10  
> (the red one in the samples), and not 65.
> 
> Is it possible to do this kind of aggregation? Or have I to change my  
> document structure (or index mapping or ...)? If it is possible, could you  
> point me how to do it? I had a look at nested aggregations and filter ones,  
> but I am not sure that it could help.
> 
> Thank you for your replies.
> 
> Regards,  
> Loï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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%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/CAL6Z4j6mCs-gBR8fOygLEcX98JUVxgqg2d%2B-3\_V\_6SBOuq3iRA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6mCs-gBR8fOygLEcX98JUVxgqg2d%2B-3_V_6SBOuq3iRA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Loic\_Wenkin](https://avatars.discourse-cdn.com/v4/letter/l/4491bb/32.png) [@Loic\_Wenkin](https://discuss.elastic.co/u/Loic_Wenkin)\
**Post date:** [January 14, 2015, 2:41pm UTC](https://discuss.elastic.co/t/filter-in-nested-aggregations/21632/3 "2015-01-14T14:41:12Z")

</div>

Hi Adrien,

Thanks a lot, it works!

Regards,  
Loïc

Le mercredi 14 janvier 2015 12:06:54 UTC+1, Adrien Grand a écrit :

> Provided that `d` is mapped as a `nested` object, you can do it by using  
> the nested aggregation. Here are the aggregations that you would need on  
> each level
> 
> 1. nested aggregation to be in the context of path='d'
> 2. filter aggregation on d.toto == 1
> 3. max aggregation on d.tutu
> 
> On Wed, Jan 14, 2015 at 11:08 AM, Loïc Wenkin \<[loic....@gmail.com](mailto:loic....@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Hi all,
> > 
> > I was wondering if it is possible to apply aggregations only on a set of  
> > nested objects (not all nested objects presents inside a document),  
> > according to a property of this object? I think an example will help to  
> > understand.
> > 
> > Let's say that you have the following documents inthe index:
> > 
> > {  
> > "a": 2,  
> > "b": 3,  
> > "c": 4,  
> > "d": [  
> > {  
> > "toto": 1,  
> > "tutu": 2,  
> > "titi": 3  
> > },  
> > {  
> > "toto": 2,  
> > "tutu": 5,  
> > "titi": 6  
> > },  
> > {  
> > "toto": 3,  
> > "tutu": 11,  
> > "titi": 8  
> > },  
> > {  
> > "toto": 4,  
> > "tutu": 7,  
> > "titi": 4  
> > }  
> > ]  
> > }
> > 
> > {  
> > "a": 3,  
> > "b": 4,  
> > "c": 5,  
> > "d": [  
> > {  
> > "toto": 1,  
> > "tutu": 10,  
> > "titi": 6  
> > },  
> > {  
> > "toto": 2,  
> > "tutu": 65,  
> > "titi": 8  
> > },  
> > {  
> > "toto": 3,  
> > "tutu": 25,  
> > "titi": 15  
> > },  
> > {  
> > "toto": 4,  
> > "tutu": 30,  
> > "titi": 45  
> > }  
> > ]  
> > }
> > 
> > Where "d" is indexed as nested. I would like to get the maximum value for  
> > the "tutu" property from this kind of documents, but I would like to work  
> > only on nested objects where "toto" = 1. Here, I would like to get 10  
> > (the red one in the samples), and not 65.
> > 
> > Is it possible to do this kind of aggregation? Or have I to change my  
> > document structure (or index mapping or ...)? If it is possible, could you  
> > point me how to do it? I had a look at nested aggregations and filter ones,  
> > but I am not sure that it could help.
> > 
> > Thank you for your replies.
> > 
> > Regards,  
> > Loï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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/681f7e7f-6340-4699-88f4-bb99032e47b5%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/9fecd1ff-4d6b-4d24-acd0-c7e6e457abc0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9fecd1ff-4d6b-4d24-acd0-c7e6e457abc0%40googlegroups.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, 12:39am UTC](https://discuss.elastic.co/t/filter-in-nested-aggregations/21632/4 "2017-07-06T00:39:00Z")

</div>


