# Custom Aggregation / Access to documents

**URL:** <https://discuss.elastic.co/t/custom-aggregation-access-to-documents/20867>\
**Category:** Elasticsearch\
**Created:** [November 20, 2014, 11:51am UTC](https://discuss.elastic.co/t/custom-aggregation-access-to-documents/20867 "2014-11-20T11:51:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndyP](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@AndyP](https://discuss.elastic.co/u/AndyP)\
**Post date:** [November 20, 2014, 11:51am UTC](https://discuss.elastic.co/t/custom-aggregation-access-to-documents/20867/1 "2014-11-20T11:51:39Z")

</div>

When implementing a custom aggregation: can I access the result documents  
in my aggregator so that I can skip result documents based on it's  
properties?

To make it clearer I explain

I have an index "products" that contains "product" documents. A product  
contains a nested collection of "variant" documents.

The requirements is to have a query that return "variant" documents. Kind  
of a "Nested Aggregation".

To complicate things: not all "variants" should be returned. Some dynamic  
filtering has to be applied. And this filtering depends on properties of  
the nested "variant" documents. I need to peek at all variants contained in  
a product in order to determine if a variant should be included in the  
result or not.

I am thinking that I could accomplish this writing a plugin which contains  
the custom aggregation when my initial question could be answered with  
"yes".

Thanks for your suggestions and insights.

A.

--  
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/7dca8221-0764-4703-8b75-0ae492961dd1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7dca8221-0764-4703-8b75-0ae492961dd1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [November 20, 2014, 1:06pm UTC](https://discuss.elastic.co/t/custom-aggregation-access-to-documents/20867/2 "2014-11-20T13:06:46Z")

</div>

Hi,

I think you should be able to achieve the functionality you need without  
writing a custom aggregation. If you use a combination of the filter  
aggregation wrapped in a nested aggregation then you should be able to  
filter the child documents ("variant") before they are returned. Then if  
you want to return the top X 'variants' you can use the top\_hits  
aggregation as a sub-aggregation of the filter aggregation.

Hope this helps,

Colin

On Thursday, 20 November 2014 11:51:40 UTC, AndyP wrote:

> When implementing a custom aggregation: can I access the result documents  
> in my aggregator so that I can skip result documents based on it's  
> properties?
> 
> To make it clearer I explain
> 
> I have an index "products" that contains "product" documents. A product  
> contains a nested collection of "variant" documents.
> 
> The requirements is to have a query that return "variant" documents. Kind  
> of a "Nested Aggregation".
> 
> To complicate things: not all "variants" should be returned. Some dynamic  
> filtering has to be applied. And this filtering depends on properties of  
> the nested "variant" documents. I need to peek at all variants contained in  
> a product in order to determine if a variant should be included in the  
> result or not.
> 
> I am thinking that I could accomplish this writing a plugin which contains  
> the custom aggregation when my initial question could be answered with  
> "yes".
> 
> Thanks for your suggestions and insights.
> 
> A.

--  
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/e122e8c9-c0df-4d6b-b91c-0b208d4e5e2e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e122e8c9-c0df-4d6b-b91c-0b208d4e5e2e%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:48am UTC](https://discuss.elastic.co/t/custom-aggregation-access-to-documents/20867/3 "2017-07-06T00:48:42Z")

</div>

Closed on July 6, 2017, 12:48am UTC


