# Re-indexing an aggregation for later use

**URL:** <https://discuss.elastic.co/t/re-indexing-an-aggregation-for-later-use/45012>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [March 21, 2016, 3:45pm UTC](https://discuss.elastic.co/t/re-indexing-an-aggregation-for-later-use/45012 "2016-03-21T15:45:36Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 23, 2016, 8:39am UTC](https://discuss.elastic.co/t/re-indexing-an-aggregation-for-later-use/45012/4 "2016-03-23T08:39:13Z")

</div>

Hey folks,

take this example for further testing

First, let's bulk index some docs

```auto
PUT /foo/bar/_bulk
{ "index" : { "_id" : "1" } }
{ "foo" : "bar" }
{ "index" : { "_id" : "2" } }
{ "foo" : "bar" }
{ "index" : { "_id" : "3" } }
{ "foo" : "baz" }
{ "index" : { "_id" : "4" } }
{ "foo" : "spam" }
{ "index" : { "_id" : "5" } }
{ "foo" : "spam" }
{ "index" : { "_id" : "6" } }
{ "foo" : "spam" }

```

After refresh, we should be able to search those and aggregate on them

```auto
GET /foo/bar/_search
{
  "size": 0,
  "aggs": {
    "the_foos": {
      "terms": {
        "field": "foo",
        "size": 10
      }
    }
  }
}

```

Let's get a watch up and running

```auto
PUT _watcher/watch/transform
{
  "input": {
    "search": {
      "request": {
        "indices": [
          "foo"
        ],
        "types": [
          "bar"
        ],
        "body": {
          "size": 0,
          "aggs": {
            "the_foos": {
              "terms": {
                "field": "foo",
                "size": 10
              }
            }
          }
        }
      }
    }
  },
  "trigger": {
    "schedule": {
      "interval": "1h"
    }
  },
  "actions": {
    "index_payload": {
      "transform": {
        "script": "return [_doc : ctx.payload.aggregations.the_foos.buckets]"
      },
      "index": {
        "index": "my-index",
        "doc_type": "my-type"
      }
    }
  }
}

```

No need to wait, execute!

```auto
POST _watcher/watch/transform/_execute

```

Knowing we ran the watch, let's check the index for new documents!

```auto
GET my-index/my-type/_search

```

On my 2.2.1 test installation this showed three documents... of course you can change the documents in your script transform to whatever you want, but this should be a start.

ID's are generated automatically here.

Hope this helps!

--Alex

---

_[View the full topic](https://discuss.elastic.co/t/re-indexing-an-aggregation-for-later-use/45012)._
