Apply a script on an aggregation result in visualization


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).

I tried to embed a script in advanced JSON like this :

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

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

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

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. 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.


1 Like

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 :slight_smile:

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:

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.


1 Like

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.


Hi Sarra, that's correct, that's not yet possible. I think this issue 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.


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