# In Kibana scripted field, return sum of two integer value

**URL:** <https://discuss.elastic.co/t/in-kibana-scripted-field-return-sum-of-two-integer-value/76762>\
**Category:** Kibana\
**Created:** [February 28, 2017, 10:41am UTC](https://discuss.elastic.co/t/in-kibana-scripted-field-return-sum-of-two-integer-value/76762 "2017-02-28T10:41:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurveer\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Gurveer\_Singh](https://discuss.elastic.co/u/Gurveer_Singh)\
**Post date:** [February 28, 2017, 10:41am UTC](https://discuss.elastic.co/t/in-kibana-scripted-field-return-sum-of-two-integer-value/76762/1 "2017-02-28T10:41:14Z")

</div>

Hello,

I am trying to sum two integer values as following, but I am getting compile error. I am not much familiar with painless syntax.

if (doc['allocated.raw'].value == 'Physically') {  
int cpu = 0  
cpu = doc['total\_cpu'].value\*2  
} else {  
cpu = 0  
}  
if (doc['server\_type.raw'].value == 'Virtual') {  
int vcpu = 0  
vcpu = vdoc['total\_cpu'].value  
} else {  
cpu = 0  
}

return cpu + vcpu

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [February 28, 2017, 11:46am UTC](https://discuss.elastic.co/t/in-kibana-scripted-field-return-sum-of-two-integer-value/76762/2 "2017-02-28T11:46:47Z")

</div>

@Gurveer_Singh Try this out and see if it gets you what you were looking for

```auto
long cpu = 0;
long vcpu = 0;
if (doc['allocated.raw'].value == 'Physically') {
  cpu = doc['total_cpu'].value*2;
}
if (doc['server_type.raw'].value == 'Virtual') {
  vcpu = doc['total_cpu'].value;
}

return cpu + vcpu;

```

A couple things to note:

- the use of semi-colons
- `cpu` and `vcpu` have to be declared outside of the if statements if they're going to be used in the return
- `vdoc` doesn't exist, and you have to use `doc`

---

<div class="post-metadata">

**Author:** ![Gurveer\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Gurveer\_Singh](https://discuss.elastic.co/u/Gurveer_Singh)\
**Post date:** [February 28, 2017, 1:52pm UTC](https://discuss.elastic.co/t/in-kibana-scripted-field-return-sum-of-two-integer-value/76762/3 "2017-02-28T13:52:10Z")

</div>

Thanks @Brandon_Kobel. It works!! Thanks for clearify things to me.

---

<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 28, 2017, 1:52pm UTC](https://discuss.elastic.co/t/in-kibana-scripted-field-return-sum-of-two-integer-value/76762/4 "2017-03-28T13:52:16Z")

</div>

Closed on March 28, 2017, 1:52pm UTC

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