In Kibana scripted field, return sum of two integer value

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

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

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

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

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