Problem in script field


(Lembared Nabil) #1

Hello,

I have a problem with script field in kibana , by the way i want count number of attribute but i must test it before calculate then do another count for another attribute and finally sum them.

Thank you.


(Lee Drengenberg) #2

You haven't shared a lot of details but I can start the conversation off by telling you that scripted fields can only work on individual documents, not groups of documents.

So if you have documents that have price and tax fields, you can create a scripted field for total_sale that adds those 2 fields together on each document. But you can't get the sum of price over multiple documents in a scripted field.


(Lembared Nabil) #3

Thank you for your reply.

First i have in Elasticsearch lot of object and i want parse this object to count field "price" then "tax", and sum this fields .

I tried this script but dont work :

int m=0;
int n=0;
float T;

for(el in doc['_source'].value){
if( doc('_source.currentStatusName').value == 'AVOIDABLE'){ m++; }
else if (doc('_source.currentStatusName').value == 'NON_AVOIDABLE'){ n++; }
else { return 0;}
}

T= m/(m+n);
return T;


(Lee Drengenberg) #4

Like I said in my previous comment, that won't work because scripted fields only operate on each individual document. The scope of your m, n, and T variables are each individual document.

You can create a scripted field that has either 1 or 0 for each document based on that currentStatusName field. And then you can do some aggregation in a visualization based on that where you could have the sum of some other field.

For example, I create a scripted field with this script;

if (doc['machine.os.raw'].value == "win xp") {
  return 1
} else {
return 0
}

(if you set your scripted field to be a String instead of a Number you could return 'AVOIDABLE' or 'NON_AVOIDABLE' instead)

I can see it works in the Discover tab;
image

And now I could create a Data Table visualization that splits the data on that field like this;

Or I can change the metric from Count to Sum of some fields such as bytes;


(Lembared Nabil) #5

I used KIBANA6.2.1 what you said but it show compiler error :

Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON_AVOIDABLE"){ return "NON_AVOIDABLE" }\nelse {return "ok"}"," ^---- HERE"],"script":"if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON_AVOIDABLE"){ return "NON_AVOIDABLE" }\nelse {return "ok"}","lang":"expression"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"test","node":"aOfYSOCNRuu_LuhyA-NnFw","reason":{"type":"script_exception","reason":"compile error","script_stack":["if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON_AVOIDABLE"){ return "NON_AVOIDABLE" }\nelse {return "ok"}"," ^---- HERE"],"script":"if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON_AVOIDABLE"){ return "NON_AVOIDABLE" }\nelse {return "ok"}","lang":"expression","caused_by":{"type":"parse_exception","reason":"unexpected character '"' on line (1) position (38)","caused_by":{"type":"lexer_no_viable_alt_exception","reason":null}}}}]},"status":500}


(Lee Drengenberg) #6

Please post your scripted field script


(Lembared Nabil) #7

Sorry for the late response
it worked when i did this :

if ( params._source.machine.os.raw == "win xp") {
return 1
} else {
return 0
}


(system) #8

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