# Script in kibana

**URL:** <https://discuss.elastic.co/t/script-in-kibana/312036>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 12, 2022, 7:26pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036 "2022-08-12T19:26:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 12, 2022, 7:26pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/1 "2022-08-12T19:26:30Z")

</div>

I am trying to use simple script but output is not desirable.

for example first row is 3444 and if I divide that by 10 it should be 344.4 but it comes out to be something different.  
why is it happening? am I using this wrong way?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/5/45d1039fedd17492ae8ce53433415638b0364bf5.png)

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

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [August 12, 2022, 8:18pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/2 "2022-08-12T20:18:17Z")

</div>

Hello, this is more of an Elasticsearch question as they can explain the nuances of shards and how scripts work on them.

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 12, 2022, 8:26pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/3 "2022-08-12T20:26:15Z")

</div>

ok change category to Elasticsearch

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 13, 2022, 11:54pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/4 "2022-08-13T23:54:31Z")

</div>

it works if I do this.

```auto
{
  "script": {
    "inline": "doc['njobs'].value / 10.0",
    "lang": "painless"
  }
}

```

But I want this in integer. how do I do this?  
output becomes 344.4 I want value to be 344

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 14, 2022, 12:45am UTC](https://discuss.elastic.co/t/script-in-kibana/312036/5 "2022-08-14T00:45:22Z")

</div>

@elasticforme If you do it as a runtime field you can set the type as an `long` see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html)

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 15, 2022, 1:41pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/6 "2022-08-15T13:41:30Z")

</div>

@stephenb  
I am not sure how to do this. Can you please give me example for this one please?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 15, 2022, 1:57pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/7 "2022-08-15T13:57:14Z")

</div>

@elasticforme

I pointed you to the docs It's almost exactly the same as you had above.

> **[Map a runtime field | Elasticsearch Guide \[8.3\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html)**

I did this on my mobile do there might be errors ... You might need more logic

```auto
"mappings": {
    "runtime": {
      "my-new-field": {
        "type": "long",
        "script": {
          "source": "emit(doc['njobs'].value / 10)"
        }
      }
    }
...

```

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 15, 2022, 2:36pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/8 "2022-08-15T14:36:19Z")

</div>

ok so seems I have to do this. I will test and try to understand how this runtime filed works. how can I use on visualization etc..

```auto
PUT daily_stats-2022
{
"mappings": {
    "runtime": {
      "njobs_10": {
        "type": "int",
        "script": {
          "source": "emit(doc['njobs'].value / 10.0)"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 15, 2022, 2:50pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/9 "2022-08-15T14:50:10Z")

</div>

Per the docs... I think you will need to be a `long`

> The `runtime` section can be any of these data types:
> 
> - `boolean`
> - `composite`
> - `date`
> - `double`
> - `geo_point`
> - `ip`
> - `keyword`
> - `long`
> - [`lookup`](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-retrieving-fields.html#lookup-runtime-fields)

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 15, 2022, 2:51pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/10 "2022-08-15T14:51:57Z")

</div>

hmm, I want that to be integer. round off. looks like not an option.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 15, 2022, 2:59pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/11 "2022-08-15T14:59:03Z")

</div>

Works for me.

 ![Screen Shot 2022-08-15 at 7.57.43 AM](https://us1.discourse-cdn.com/elastic/original/3X/9/6/96adfc3d154a65af165e4624a327f379964a09d7.png)

Divide by 10 not 10.0 otherwise it will make it a double.

```auto
PUT daily_stats-2022
{
"mappings": {
    "runtime": {
      "njobs_10": {
        "type": "long",
        "script": {
          "source": "emit(doc['njobs'].value / 10)"
        }
      }
    }
  }
}

```

BTW this drops the remainder if you want to round up / down you will need to add some logic..

this worked

```auto
double remainder = doc['snmp_status'].value % 10.0;
if (remainder >= 5) {
    emit((doc['snmp_status'].value / 10) + 1)
}
else{
    emit((doc['snmp_status'].value / 10))
}

```

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [August 15, 2022, 5:14pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/12 "2022-08-15T17:14:32Z")

</div>

@stephenb  
Thank you very much. it does works. Super

---

<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:** [September 12, 2022, 5:14pm UTC](https://discuss.elastic.co/t/script-in-kibana/312036/13 "2022-09-12T17:14:36Z")

</div>

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