# How to execute scripts in Elastic aggregation

**URL:** <https://discuss.elastic.co/t/how-to-execute-scripts-in-elastic-aggregation/222037>\
**Category:** Elasticsearch\
**Created:** [March 4, 2020, 10:20am UTC](https://discuss.elastic.co/t/how-to-execute-scripts-in-elastic-aggregation/222037 "2020-03-04T10:20:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathlee](https://avatars.discourse-cdn.com/v4/letter/n/ac8455/32.png) [@nathlee](https://discuss.elastic.co/u/nathlee)\
**Post date:** [March 4, 2020, 10:20am UTC](https://discuss.elastic.co/t/how-to-execute-scripts-in-elastic-aggregation/222037/1 "2020-03-04T10:20:55Z")

</div>

I queried elasticsearch use aggregation My response is

```auto
 "aggregations" : {
    "time" : {
      "buckets" : [
        {
          "key_as_string" : "2019-01-01",
          "key" : 1546300800000,
          "doc_count" : 12,
          "look_up" : {
            "doc_count" : 5,
            "unlock_not_suggested" : {
              "doc_count" : 2
            }
          }
        },

```

. Now I want to use script to transform data like this

```auto
['lookup': 5, 'unlock_not_suggested': 2]

```

I find and see an advice use script like this but i don't know how to add this in my querry

```auto
aggs.stream().flatMap(a -> a.stream().flatMap( b->b.stream().map( c-> -> ['A': a.key, 'B': b.key, 'C': c.key] ) ) ).collect(Collectors.toList())

```

My query is

```auto
POST test.userstats/_search
{
    "size": 0,
    "aggs": {
        "time": {
            "date_histogram": {
                "field": "event_stats.watch_10_percent_video.first_time",
                "format": "yyyy-MM-dd",
                "interval": "year",
                "min_doc_count": 0
            },
                    "aggs": {
                        "look_up": {
                            "filter": {
                                "exists": {
                                    "field": "event_stats.look_up"
                                }
                            },
                            "aggs": {
                                "unlock_not_suggested": {
                                    "filter": {
                                        "exists": {
                                            "field": "event_stats.unlock_not_suggested_video"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

```

---

<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 1, 2020, 10:21am UTC](https://discuss.elastic.co/t/how-to-execute-scripts-in-elastic-aggregation/222037/2 "2020-04-01T10:21:00Z")

</div>

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