# Updating source with aggregation values

**URL:** <https://discuss.elastic.co/t/updating-source-with-aggregation-values/138915>\
**Category:** Elasticsearch\
**Created:** [July 6, 2018, 12:53pm UTC](https://discuss.elastic.co/t/updating-source-with-aggregation-values/138915 "2018-07-06T12:53:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![uyth](https://avatars.discourse-cdn.com/v4/letter/u/6a8cbe/32.png) [@uyth](https://discuss.elastic.co/u/uyth)\
**Post date:** [July 6, 2018, 12:53pm UTC](https://discuss.elastic.co/t/updating-source-with-aggregation-values/138915/1 "2018-07-06T12:53:37Z")

</div>

Using Elasticsearch v. 6.2.4, I want to update a field in my document based on multiple aggregations done on a nested object. Something like this:

```
POST <my_index>/_doc/<doc_id>/_update
{
  "script": {
    "lang": "painless", 
    "source": """
      ctx._source.field_to_update = my_aggregation_value;
                  """,
    "params": {
      "my_aggregation_value": <something>
    }
  },
    "aggs": {
    "<nested_object>": {
      "nested": {
        "path": "<nested_object>"
      },
      "aggs": {
        "<time_range>": {
          "filter": {
            "range": {
              "history.request_time": {
                "gte": "now-7d/d",
                "lte": "now"
              }
            }
          },
          "aggs": {
            "bucket_by_day": {
              "date_histogram": {
                "field": "<nested_object>.<time_event>",
                "interval": "day"
              },
              "aggs": {
                "my_aggregation": {
                  "aggregation_type": {...}
                }
              }
            }
          }
        }
      }
    }
  }
}

```

So I have three problems:

1. How do I update my document fields with aggregation values? Are there any way to set variables in params to aggregation values.
2. How can I update the root with aggregations in nested objects?
3. How can I select the last aggregation of the buckets?

---

<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:** [August 3, 2018, 12:53pm UTC](https://discuss.elastic.co/t/updating-source-with-aggregation-values/138915/2 "2018-08-03T12:53:40Z")

</div>

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