# Use result of an aggregation query to enrich doc

**URL:** <https://discuss.elastic.co/t/use-result-of-an-aggregation-query-to-enrich-doc/361130>\
**Category:** Elasticsearch\
**Created:** [June 10, 2024, 9:02am UTC](https://discuss.elastic.co/t/use-result-of-an-aggregation-query-to-enrich-doc/361130 "2024-06-10T09:02:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanC](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@StefanC](https://discuss.elastic.co/u/StefanC)\
**Post date:** [June 10, 2024, 9:02am UTC](https://discuss.elastic.co/t/use-result-of-an-aggregation-query-to-enrich-doc/361130/1 "2024-06-10T09:02:46Z")

</div>

Is its possible to execute an aggregation query on an index and use the results in one action to enrich the same index/documents with it.

Assume i have serveral document with field region, sensor type sensor. I want to add to each document the number of times the sensor triggered, the number of times it was a certain sensor type and how many times it happend in a region. The aggregation of counting the values are in time frames of 5min.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 10, 2024, 9:26am UTC](https://discuss.elastic.co/t/use-result-of-an-aggregation-query-to-enrich-doc/361130/2 "2024-06-10T09:26:11Z")

</div>

It's looking a bit similar to what downsampling does: [Downsampling a time series data stream | Elasticsearch Guide [8.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.14/downsampling.html)

But to answer your exact question, no I don't think you can inside Elasticsearch do that automatically. But you can write a piece of code which does that I guess. Or look at downsampling to see if it helps.
