# How to calculate facet over parent documents?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-facet-over-parent-documents/13862>\
**Category:** Elasticsearch\
**Created:** [October 6, 2013, 10:11pm UTC](https://discuss.elastic.co/t/how-to-calculate-facet-over-parent-documents/13862 "2013-10-06T22:11:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joerg\_reichardt](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@joerg\_reichardt](https://discuss.elastic.co/u/joerg_reichardt)\
**Post date:** [October 6, 2013, 10:11pm UTC](https://discuss.elastic.co/t/how-to-calculate-facet-over-parent-documents/13862/1 "2013-10-06T22:11:32Z")

</div>

Hi all,

I'd like to calculate the statistics of an event stream in which only a  
limited number of possible events occur. I've modeled this as an item type  
with some properties, and an event type that has an item as parent and only  
stores the time of the event. The number of items (possible events) is  
much, much smaller than the number of events.

I'd now like to calculate statistics for the events. Consider the following  
example: 4 items and 7 events

curl -XPUT localhost:9200/test/item/1  
-d'{"name":"A","color":"blue","weight":3}'  
curl -XPUT localhost:9200/test/item/2  
-d'{"name":"B","color":"red","weight":6}'  
curl -XPUT localhost:9200/test/item/3  
-d'{"name":"C","color":"blue","weight":3}'  
curl -XPUT localhost:9200/test/item/4  
-d'{"name":"D","color":"green","weight":2}'

curl -XPOST localhost:9200/test/event/\_mapping  
-d'{"event":{"\_parent":{"type":"item"}}}'

curl -XPOST localhost:9200/test/event?parent=1 -d'{"day":"Mon"}'  
curl -XPOST localhost:9200/test/event?parent=2 -d'{"day":"Mon"}'  
curl -XPOST localhost:9200/test/event?parent=3 -d'{"day":"Mon"}'  
curl -XPOST localhost:9200/test/event?parent=3 -d'{"day":"Mon"}'  
curl -XPOST localhost:9200/test/event?parent=4 -d'{"day":"Tue"}'  
curl -XPOST localhost:9200/test/event?parent=1 -d'{"day":"Tue"}'  
curl -XPOST localhost:9200/test/event?parent=1 -d'{"day":"Tue"}'

How can I calculate the average weight for all events occurring on Monday  
(15/4=3.75)?  
More general, I'd like to be able to filter the events by day and then  
calculate a (statistial) facet of item properties over these events.

I know I could repeat the item data in each event and the problem would be  
solved, i.e. for each incoming event, query the item properties and  
replicate them again, but that would require one read of an item for ever  
write of an event plus require to re-index an item with every event. That  
appears to be silly.

Any help is greatly appreciated!

--  
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).  
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, 2:13am UTC](https://discuss.elastic.co/t/how-to-calculate-facet-over-parent-documents/13862/2 "2017-07-06T02:13:29Z")

</div>

Closed on July 6, 2017, 2:13am UTC


