Handling empty/zero values in Scripted Fields

version 2.4
Below are my two epoch times in Number format
doc['actual'].value-doc['expected'].value < 0?1:0

I'm looking for a percentage of actual time not meeting the expected time. But I have challenge where few documents don't have both of these values or missing either of the values which I want to ignore.

Any inputs is greatly appreciated.

If a field is missing in some docs, you can check for its existence before using it in your script with doc.containsKey('actual')

1 Like

Thank you Bargs. I tried the below
(doc.containsKey['actual']?doc['actual'].value)-(doc.containsKey['expected']?doc['expected'].value) < 0?1:0

I keep getting the error "parse_exception: unexpected token ')' on line (1)"
Am I doing it right?

If one of the fields (actual or expected) is missing, what would you like to do? Do you want to use a default value for the missing field, or do you want to skip the calculation entirely and return a default value for the script?

I want to skip the calculation entirely

Ok, you'll want to do something like this then:

if(doc.containsKey('actual') && doc.containsKey('expected')) {
  // do calculation
else {
  // return default value
1 Like

I get the below error
{"error":{"root_cause":[{"type":"lexer_no_viable_alt_exception","reason":"lexer_no_viable_alt_exception: null"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"test","node":"gZEsj74DTtaFMe-aG2","reason":{"type":"script_exception","reason":"Failed to compile inline script [if(doc.containsKey('actual') && doc.containsKey('expected')) {doc['actual'].value - doc['expected'].value < 0?1:0} else {return -1}] using lang [expression]","caused_by":{"type":"script_exception","reason":"Failed to parse expression: if(doc.containsKey('actual') && doc.containsKey('expected')) {doc['actual'].value - doc['expected'].value < 0?1:0} else {return -1}","caused_by":{"type":"parse_exception","reason":"parse_exception: unexpected character ''' on line (1) position (19)","caused_by":{"type":"lexer_no_viable_alt_exception","reason":"lexer_no_viable_alt_exception: null"}}}}}]}}

I'm so sorry, I totally missed that you were working with 2.4. I assumed you were using the new Painless language in 5.x. The syntax will be a little different with lucene expressions:

(doc['actual'].empty || doc['expected'].empty) 
? <your-chosen-default-value> 
: doc['actual'].value - doc['expected'].value < 0 ? 1 : 0
1 Like

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