# Scripted field on Aggregated value

**URL:** <https://discuss.elastic.co/t/scripted-field-on-aggregated-value/87157>\
**Category:** Kibana\
**Created:** [May 25, 2017, 4:53pm UTC](https://discuss.elastic.co/t/scripted-field-on-aggregated-value/87157 "2017-05-25T16:53:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna\_chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krishna\_chaitanya](https://discuss.elastic.co/u/krishna_chaitanya)\
**Post date:** [May 25, 2017, 4:53pm UTC](https://discuss.elastic.co/t/scripted-field-on-aggregated-value/87157/1 "2017-05-25T16:53:50Z")

</div>

Scripted fields can work on a field or set of fields in the document. I would like to create scripted field on aggregated value.

Say, I have terms aggregation of `fieldA` with count metric.  
I would like to create another field based on the value of count, which says:

if count \<= value, **scripted\_field** = 1,  
if value1\<= count \<=value2, **scripted\_field** =2,  
....  
how can I achieve this in Kibana?

Also, finally have a data table like this: Is it possible?

```
 fieldA Count scripted_field
  ..... ..... ......
  ..... ..... ......
```

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [May 26, 2017, 7:34pm UTC](https://discuss.elastic.co/t/scripted-field-on-aggregated-value/87157/2 "2017-05-26T19:34:25Z")

</div>

Hi Krishna,

Scripted fields work only on values within each individual doc. So if a doc had 3 numeric fields you could use a scripted field to add them together, or average them, or find the min, max, etc. But not across all docs or a set of docs.

But you can also use some scripting within Visualizations in the Advanced JSON field;

 ![](https://us1.discourse-cdn.com/elastic/original/3X/6/2/62a700a9bf30d17b28493c542ae0bfd5a3ade740.png)

I don't know of very many exampels of using it, but here's one;

> [@Display concurrency in data on Kibana](https://discuss.elastic.co/t/display-concurrency-in-data-on-kibana/26006/13):
>
> It worked for me with the following data and config: curl -XPOST 'http://localhost:9200/test3' -d '{ "settings" : { "number\_of\_shards" : 1 }, "mappings" : { "test" : { "properties" : { "start\_date" : { "type" : "date"}, "end\_date" : { "type" : "date"}, "duration" : { "type" : "integer"} } } } }' curl -XPUT 'http://localhost:9200/test3/test/1' -d '{ "start\_date" : "2015-08-23T00:0…

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![krishna\_chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krishna\_chaitanya](https://discuss.elastic.co/u/krishna_chaitanya)\
**Post date:** [May 30, 2017, 1:36pm UTC](https://discuss.elastic.co/t/scripted-field-on-aggregated-value/87157/3 "2017-05-30T13:36:55Z")

</div>

Thanks for the response, Lee

---

<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:** [June 27, 2017, 1:37pm UTC](https://discuss.elastic.co/t/scripted-field-on-aggregated-value/87157/4 "2017-06-27T13:37:06Z")

</div>

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