# Aggregating data from child documents in has\_child query

**URL:** <https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935>\
**Category:** Elasticsearch\
**Created:** [February 16, 2011, 3:35pm UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935 "2011-02-16T15:35:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wojciech\_Durczynski](https://avatars.discourse-cdn.com/v4/letter/w/fbc32d/32.png) [@Wojciech\_Durczynski](https://discuss.elastic.co/u/Wojciech_Durczynski)\
**Post date:** [February 16, 2011, 3:35pm UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935/1 "2011-02-16T15:35:11Z")

</div>

Hello.

I'd like to search all parent documents that have child documents in  
index, but also would like to retrieve aggregated data about child  
documents for found parents.

For example: every child document has price defined as a field and I'd  
like to query parent documents that have any children and in search  
results I'd like to receive also minimum price value for every parent  
(aggregated from this parent's children).

I tried:

- statistical facet with child scope, but it returns global minimum  
price (not in context of parent)
- terms facet with script that was concatenating parent id with price  
and from results of this facet I was able to aggregate minimum prices  
for each parent
- histogram script facet but keys can only be numbers (not parent ids)  
and count can only be total/mean of values and not minimum

What is the best way to achieve desired functionality?

Regards  
Wojciech Durczyński

---

<div class="post-metadata">

**Author:** ![Wojciech\_Durczynski](https://avatars.discourse-cdn.com/v4/letter/w/fbc32d/32.png) [@Wojciech\_Durczynski](https://discuss.elastic.co/u/Wojciech_Durczynski)\
**Post date:** [February 16, 2011, 3:52pm UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935/2 "2011-02-16T15:52:13Z")

</div>

One more thing: I cannot add minimum price as a field in parent  
document, because children are filtered using various conditions.

On Feb 16, 4:35 pm, Wojciech Durczyński  
[wojciech.durczyn...@comarch.com](mailto:wojciech.durczyn...@comarch.com) wrote:

> Hello.
> 
> I'd like to search all parent documents that have child documents in  
> index, but also would like to retrieve aggregated data about child  
> documents for found parents.
> 
> For example: every child document has price defined as a field and I'd  
> like to query parent documents that have any children and in search  
> results I'd like to receive also minimum price value for every parent  
> (aggregated from this parent's children).
> 
> I tried:
> 
> - statistical facet with child scope, but it returns global minimum  
> price (not in context of parent)
> - terms facet with script that was concatenating parent id with price  
> and from results of this facet I was able to aggregate minimum prices  
> for each parent
> - histogram script facet but keys can only be numbers (not parent ids)  
> and count can only be total/mean of values and not minimum
> 
> What is the best way to achieve desired functionality?
> 
> Regards  
> Wojciech Durczyński

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 17, 2011, 1:07am UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935/3 "2011-02-17T01:07:21Z")

</div>

If I understood correctly, and you want to get facets per parent, then you can't do it.  
On Wednesday, February 16, 2011 at 5:52 PM, Wojciech DurczyÅski wrote:

> One more thing: I cannot add minimum price as a field in parent  
> document, because children are filtered using various conditions.
> 
> On Feb 16, 4:35 pm, Wojciech DurczyÅski  
> [wojciech.durczyn...@comarch.com](mailto:wojciech.durczyn...@comarch.com) wrote:
> 
> > Hello.
> > 
> > I'd like to search all parent documents that have child documents in  
> > index, but also would like to retrieve aggregated data about child  
> > documents for found parents.
> > 
> > For example: every child document has price defined as a field and I'd  
> > like to query parent documents that have any children and in search  
> > results I'd like to receive also minimum price value for every parent  
> > (aggregated from this parent's children).
> > 
> > I tried:
> > 
> > - statistical facet with child scope, but it returns global minimum  
> > price (not in context of parent)
> > - terms facet with script that was concatenating parent id with price  
> > and from results of this facet I was able to aggregate minimum prices  
> > for each parent
> > - histogram script facet but keys can only be numbers (not parent ids)  
> > and count can only be total/mean of values and not minimum
> > 
> > What is the best way to achieve desired functionality?
> > 
> > Regards  
> > Wojciech DurczyÅski

---

<div class="post-metadata">

**Author:** ![srrin](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@srrin](https://discuss.elastic.co/u/srrin)\
**Post date:** [March 16, 2011, 1:11pm UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935/4 "2011-03-16T13:11:56Z")

</div>

Hi Shay,  
Do you have any plan for this feature to be incorporated in upcoming versions?

Thanks  
-SRR

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 16, 2011, 7:45pm UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935/5 "2011-03-16T19:45:30Z")

</div>

No, no direct plan, mainly because its not simple (/possible) to do it in a very efficient manner when using child / parent. You might need to change the model to support what you want.  
On Wednesday, March 16, 2011 at 3:11 PM, srrIN wrote:

> Hi Shay,  
> Do you have any plan for this feature to be incorporated in upcoming  
> versions?
> 
> Thanks  
> -SRR
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Aggregating-data-from-child-documents-in-has-child-query-tp2510238p2688162.html](http://elasticsearch-users.115913.n3.nabble.com/Aggregating-data-from-child-documents-in-has-child-query-tp2510238p2688162.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:09am UTC](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935/6 "2017-07-06T04:09:52Z")

</div>


