# Apply a script on an aggregation result in visualization

**URL:** <https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052>\
**Category:** Kibana\
**Created:** [November 9, 2017, 2:08pm UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052 "2017-11-09T14:08:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarra\_Bouhenni](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Sarra\_Bouhenni](https://discuss.elastic.co/u/Sarra_Bouhenni)\
**Post date:** [November 9, 2017, 2:08pm UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/1 "2017-11-09T14:08:16Z")

</div>

Hello,

I have a visualization on Kibana of type "Data table", I need to apply a simple script on the count aggregations result (divied count by 2).

 ![Capture1](https://us1.discourse-cdn.com/elastic/original/3X/1/2/12976187f729726aad9067717a6b04b96da5bd0d.PNG)  
I tried to embed a script in advanced JSON like this :

```auto
{ "script" : "doc['count'].value / 2" }

```

But, it doesn't like having any affect on my results

 ![table](https://us1.discourse-cdn.com/elastic/original/3X/2/9/291d5058973b5e4dea80cf55a9028da765105c4a.PNG)

Any thought about how to make it ?  
Thanks in advance for your help !

---

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [November 10, 2017, 3:51am UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/2 "2017-11-10T03:51:11Z")

</div>

Hi there, thank you for posting your question. The way the Advanced JSON input works is that it allows you to customize the request sent to Elasticsearch by adding parameters to the [aggregation configuration](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html). The script parameter defines an inline script, which operates against each _document_, and not the result of the aggregation. So you can't use it to accomplish what you're trying to do.

Are you simply trying to "resize" your visualization? If I can get a better idea of the problem you're trying to solve I can try to suggest an alternate solution.

Thanks,  
CJ

---

<div class="post-metadata">

**Author:** ![Sarra\_Bouhenni](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Sarra\_Bouhenni](https://discuss.elastic.co/u/Sarra_Bouhenni)\
**Post date:** [November 10, 2017, 12:58pm UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/3 "2017-11-10T12:58:27Z")

</div>

Hi CJ,

Thank you for your answer, let me explain.  
I need to create a third column on my data table which should be the result of applying a script on my second column.

**First column** : driver\_id  
**Second column** : group by driver\_id (display doc\_count)  
**Third colum** : apply a script with the second column as a parameter.

In the abode post I wanted to only divide the doc\_count by 2, but I may need to apply a more complicated scripts.

So this is it, I wish I was more clear now 🙂

---

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [November 10, 2017, 10:46pm UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/4 "2017-11-10T22:46:02Z")

</div>

I see! Well, as I mentioned you can write a Painless script to perform changes to the values in your documents which will affect the end result of the aggregation. Here's an example of what I mean:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/c/3cc6d4daeeddeed66185de0cb4b67a6c0bfe5844.png)

Here, I've defined a script which divides the "bytes" field value by 2 before it's aggregated. You can see the result in the 3rd column of the data table. Does this help? It's tough to give a more precise suggestion without knowing exactly the kind of script you need, but hopefully this provides a starting point.

CJ

---

<div class="post-metadata">

**Author:** ![Sarra\_Bouhenni](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@Sarra\_Bouhenni](https://discuss.elastic.co/u/Sarra_Bouhenni)\
**Post date:** [November 11, 2017, 11:28am UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/5 "2017-11-11T11:28:10Z")

</div>

Thank you CJ for the help, now I see how to apply scripts before aggregating!

But sometimes we need to apply bucket scripts, I guess they are not yet possible on the available visualizations.

Sarra.

---

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [November 13, 2017, 4:50pm UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/6 "2017-11-13T16:50:35Z")

</div>

Hi Sarra, that's correct, that's not yet possible. I think [this issue](https://github.com/elastic/kibana/issues/2646) is relevant to what you're asking for, so you can subscribe to it if you would like to track progress on such a feature.

Thanks,  
CJ

---

<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 11, 2017, 4:50pm UTC](https://discuss.elastic.co/t/apply-a-script-on-an-aggregation-result-in-visualization/107052/7 "2017-12-11T16:50:39Z")

</div>

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