# Assign new field in one item based on aggregation

**URL:** <https://discuss.elastic.co/t/assign-new-field-in-one-item-based-on-aggregation/173789>\
**Category:** Elasticsearch\
**Created:** [March 25, 2019, 3:39pm UTC](https://discuss.elastic.co/t/assign-new-field-in-one-item-based-on-aggregation/173789 "2019-03-25T15:39:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stschelm](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@stschelm](https://discuss.elastic.co/u/stschelm)\
**Post date:** [March 25, 2019, 3:39pm UTC](https://discuss.elastic.co/t/assign-new-field-in-one-item-based-on-aggregation/173789/1 "2019-03-25T15:39:17Z")

</div>

Hi there,  
I have some log entries in elasticserarch, based on a well-defined start message and up to three follow-up messages (each an individual doc).  
I want to aggregate over a time field for items with the same unique message id.  
The following aggregation generates the desired two values (total hits from search and the total duration based on the aggregation):

> {  
> "query": {  
> "bool": {  
> "must": {"match\_all": {}},  
> "filter": {  
> "term": {"message\_id": }  
> }  
> }  
> },  
> "aggs": {"total\_duration": {"sum":{"field": "duration"}}}  
> }

Now I need two things:

1. Do this for all available ids
2. Save count and total duration into two new fields in the starting message item

I looked at scripts, scripted fields, update with query and other things but I couldn't find the combination I need.  
Any pointers and examples would be appreciated.

---

<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:** [April 22, 2019, 3:39pm UTC](https://discuss.elastic.co/t/assign-new-field-in-one-item-based-on-aggregation/173789/2 "2019-04-22T15:39:24Z")

</div>

Closed on April 22, 2019, 3:39pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
