# Aggregation on parent/child documents

**URL:** <https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949>\
**Category:** Elasticsearch\
**Created:** [February 21, 2014, 12:43pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949 "2014-02-21T12:43:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Augusto\_Uehara](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Augusto\_Uehara](https://discuss.elastic.co/u/Augusto_Uehara)\
**Post date:** [February 21, 2014, 12:43pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949/1 "2014-02-21T12:43:07Z")

</div>

Hello ElasticSearchers!

I've been working on a BI system with ES 0.90 and we needed count "users"  
which have certain attributes, for instance let's say gender and star sign.  
A user is a parent-level document and the attributes are child documents.

Fro the sample above, we were doing so by creating a query for each  
combination of male / female and the star signs and querying individually,  
as one can imagine, this was slow, but the results are exactly what we  
want. We could run this in roughly 2 minutes.

We considered using the msearch query to get these results in a single  
query and we ended up with something similar to this:  
[https://gist.github.com/chaos-generator/9133118](https://gist.github.com/chaos-generator/9133118)  
The sample above runs in 40 seconds give or take.

And along came elastic search 1.0.0 and now we have aggregations, so we  
simplified our query to this:  
[https://gist.github.com/chaos-generator/9133139](https://gist.github.com/chaos-generator/9133139)  
This runs lightning fast and we get the results in 200ms on average, which  
is ideal for us, BUT we get the total number of documents with the  
attributes, rather than the count on the parent documents.

Our problem, as you can see in the msearch gist, is that we have a parent  
level document and child documents, which would only be updated if another  
document with the exact same attributes came in, this means that a parent  
level user document can have three child documents that will have gender  
and star sign, but I only want to count the parent document, rather than  
each individual child document.

As we don't know in advance the attributes our users will be searching, we  
cannot use a script in index time to help us do this aggregation. We tried  
to use a script in search time like this:  
[https://gist.github.com/chaos-generator/9133321](https://gist.github.com/chaos-generator/9133321) , but it didn't work as we  
wanted too:

Any suggestions would be greatly appreciated.

Best regards,  
Augusto

--  
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/3d9a0683-efa3-4398-b546-087005ab2b67%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3d9a0683-efa3-4398-b546-087005ab2b67%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 21, 2014, 4:48pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949/2 "2014-02-21T16:48:56Z")

</div>

I'm wondering if the filter aggregation will work for you:

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html)

However, it does not support parent child, but if you have the children  
embedded directly inside the parent document, I think it should be similar  
in functionality to your \_msearch solution.

BTW, if you are only doing aggregations or counts and don't really need  
search hits returned, you can further optimize by using the count search  
type:

\_search?search\_type=count

--  
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/262d7ef2-e794-4927-b6d9-cb021fee3b00%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/262d7ef2-e794-4927-b6d9-cb021fee3b00%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Augusto\_Uehara](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Augusto\_Uehara](https://discuss.elastic.co/u/Augusto_Uehara)\
**Post date:** [February 21, 2014, 7:30pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949/3 "2014-02-21T19:30:52Z")

</div>

Thank you for your reply Binh,

I've tried the bucket filter, but had problems with parent/child  
relationships.

I've modified the multi-search query to use type = count, but the  
performance didn't change much, it took about 40 seconds to return the  
results. It was almost 20% faster indeed, but it is not the performance we  
want yet.

--  
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/569b670e-d1d7-4edb-81bf-199f24cce552%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/569b670e-d1d7-4edb-81bf-199f24cce552%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Augusto\_Uehara](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Augusto\_Uehara](https://discuss.elastic.co/u/Augusto_Uehara)\
**Post date:** [February 25, 2014, 1:33pm UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949/4 "2014-02-25T13:33:05Z")

</div>

We run 4 instances of ES 1.0.0 using 30G for JVM. We run 64-bit OpenJDK  
1.7.0\_25 on ubuntu servers.

$ ulimit -a  
core file size (blocks, -c) 0  
data seg size (kbytes, -d) unlimited  
scheduling priority (-e) 0  
file size (blocks, -f) unlimited  
pending signals (-i) 515139  
max locked memory (kbytes, -l) unlimited  
max memory size (kbytes, -m) unlimited  
open files (-n) 64000  
pipe size (512 bytes, -p) 8  
POSIX message queues (bytes, -q) 819200  
real-time priority (-r) 0  
stack size (kbytes, -s) 8192  
cpu time (seconds, -t) unlimited  
max user processes (-u) 515139  
virtual memory (kbytes, -v) unlimited  
file locks (-x) unlimited

And I also disabled swap on linux.

You can use this gist to simulate the issue we have:  
[https://gist.github.com/chaos-generator/9143655](https://gist.github.com/chaos-generator/9143655)

--  
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/a6db68fc-a7c8-43af-bbc4-59a0866aba36%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a6db68fc-a7c8-43af-bbc4-59a0866aba36%40googlegroups.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, 1:47am UTC](https://discuss.elastic.co/t/aggregation-on-parent-child-documents/15949/5 "2017-07-06T01:47:26Z")

</div>


