# Assign the result of aggregation to a field

**URL:** <https://discuss.elastic.co/t/assign-the-result-of-aggregation-to-a-field/107186>\
**Category:** Elasticsearch\
**Created:** [November 10, 2017, 12:41pm UTC](https://discuss.elastic.co/t/assign-the-result-of-aggregation-to-a-field/107186 "2017-11-10T12:41:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmbtenorio](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@mmbtenorio](https://discuss.elastic.co/u/mmbtenorio)\
**Post date:** [November 10, 2017, 12:41pm UTC](https://discuss.elastic.co/t/assign-the-result-of-aggregation-to-a-field/107186/1 "2017-11-10T12:41:38Z")

</div>

I wanted to transfer some data to a new index. I used a watcher to do it. The thing is, I can't retrieve the values and assign it to its corresponding field. Here's my watcher query

```
PUT _xpack/watcher/watch/transform
{
  "input": {
    "search": {
      "request": {
        "indices": [
          "source_index"
        ],
        "body": {
          "size": 0,
          "aggs": {
            "projects": {
              "terms": {
                "field": "project_number"
              },
              "aggs": {
                "total_sum": {
                  "sum": {
                    "field": "amount"
                  }
                },
                "accounts": {
                  "terms": {
                    "field": "account_id"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "trigger": {
    "schedule": {
      "interval": "1h"
    }
  },
  "actions": {
    "index_payload": {
      "transform": {
        "script": {
          "inline": "return ['_doc' : ctx.payload.aggregations.projects.buckets.key]; "
          
        }
      },
      "index": {
        "index": "new_index",
        "doc_type": "type1"
      }
    }
  }
}

```

The data are transferred to the index, but also in an aggregated result. I'm wondering if it's possible to separate it's result and assign them to different fields.

![Aggregation Result](https://us1.discourse-cdn.com/elastic/original/3X/f/8/f8b602aa3e007a89b4f923df4b9eb6ddbeff9073.JPG)

 ![Aggs Result Json](https://us1.discourse-cdn.com/elastic/original/3X/c/e/ced1acade98fd4c8bf24431380ab1c38c885614f.JPG)

Any input is highly 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:** [December 8, 2017, 12:41pm UTC](https://discuss.elastic.co/t/assign-the-result-of-aggregation-to-a-field/107186/2 "2017-12-08T12:41:39Z")

</div>

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