# Scripted\_metric in Kibana visualization

**URL:** <https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008>\
**Category:** Kibana\
**Created:** [February 22, 2017, 9:52am UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008 "2017-02-22T09:52:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elk\_user1](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@elk\_user1](https://discuss.elastic.co/u/elk_user1)\
**Post date:** [February 22, 2017, 9:52am UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/1 "2017-02-22T09:52:43Z")

</div>

Hi All,

I wanted to know if there is any support for using scripted\_metric aggregations in Kibana ? I wrote an elasticsearch query which uses scripted\_metric, but I want to use it in visualizations. I am using Kibana/ElasticSearch versions 5.1.1.. You help will be really grateful.

Thanks,  
Deepti

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [February 22, 2017, 8:35pm UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/2 "2017-02-22T20:35:53Z")

</div>

Could you provide the script you're using so we can see if there is a good way to accomplish it in Kibana?

---

<div class="post-metadata">

**Author:** ![elk\_user1](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@elk\_user1](https://discuss.elastic.co/u/elk_user1)\
**Post date:** [February 23, 2017, 4:33am UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/3 "2017-02-23T04:33:27Z")

</div>

Hello,

Thanks for replying. I am trying to find out the time spent on a test based on started and ended object.id (assuming for any student for a particular test, there are only two events : start and end)  
Below is the scripted metric aggregation query.

> GET test-data/\_search  
> {  
> "from" : 0,  
> "size" : 0,  
> "aggregations" : {  
> "user-id" : {  
> "terms" : {  
> "field" : "user-id",  
> "size" : 200,  
> "shard\_size" : -1,  
> "min\_doc\_count" : 1,  
> "shard\_min\_doc\_count" : 0,  
> "show\_term\_doc\_count\_error" : false,  
> "order" : [  
> {  
> "\_count" : "desc"  
> },  
> {  
> "\_term" : "asc"  
> }  
> ]  
> },  
> "aggregations" : {  
> "assessment\_id" : {  
> "terms" : {  
> "field" : "testid",  
> "size" : 10,  
> "shard\_size" : -1,  
> "min\_doc\_count" : 1,  
> "shard\_min\_doc\_count" : 0,  
> "show\_term\_doc\_count\_error" : false,  
> "order" : [  
> {  
> "\_count" : "desc"  
> },  
> {  
> "\_term" : "asc"  
> }  
> ]  
> },  
> "aggregations" : {  
> "timeontask": {  
> "scripted\_metric": {  
> "init\_script" : "params.\_agg.events = ",  
> "map\_script" : "def sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); def t = sf.parse(sf.format(doc['timestamp'].value)).getTime(); params.\_agg.events.add(doc['object.id'].contains('started') ? -1 \* t : t) ",   
> "combine\_script" : "double totTime=0 ; for (t in params.\_agg.events) { totTime += t } return totTime",  
> "reduce\_script" : "double totTime=0 ; for (a in params.\_aggs) { totTime += a } return totTime/60000 "  
> }  
> }  
> }  
> }  
> }  
> }  
> },  
> "ext" : { }  
> }

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [February 23, 2017, 10:42pm UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/4 "2017-02-23T22:42:46Z")

</div>

Unfortunately there isn't any way to accomplish this in Kibana. There's an open issue you can add your feedback to and follow for notifications here: [https://github.com/elastic/kibana/issues/7879](https://github.com/elastic/kibana/issues/7879)

---

<div class="post-metadata">

**Author:** ![elk\_user1](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@elk\_user1](https://discuss.elastic.co/u/elk_user1)\
**Post date:** [February 27, 2017, 7:39am UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/5 "2017-02-27T07:39:51Z")

</div>

Hi,

Is there a way to post these aggregated results to a new index in elasticsearch?  
Probably I can create some visuals using new index fields. Just a thought.

Thanks,  
Deepti Antony

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [February 27, 2017, 4:10pm UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/6 "2017-02-27T16:10:24Z")

</div>

Hmm, I don't know of anything to allow you to do that. There is a [reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html) that allows using scripts, but from my understanding, the scripts execute on the document level, not on aggregations.

That being said, there's nothing preventing you from taking the results of the aggregation and manually indexing them into a new index.

---

<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:** [March 27, 2017, 4:10pm UTC](https://discuss.elastic.co/t/scripted-metric-in-kibana-visualization/76008/7 "2017-03-27T16:10:36Z")

</div>

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