# Handling empty/zero values in Scripted Fields

**URL:** <https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600>\
**Category:** Kibana\
**Created:** [June 15, 2017, 5:51pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600 "2017-06-15T17:51:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thinkdffrnt](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@thinkdffrnt](https://discuss.elastic.co/u/thinkdffrnt)\
**Post date:** [June 15, 2017, 5:51pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/1 "2017-06-15T17:51:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [June 15, 2017, 9:07pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/2 "2017-06-15T21:07:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thinkdffrnt](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@thinkdffrnt](https://discuss.elastic.co/u/thinkdffrnt)\
**Post date:** [June 15, 2017, 10:28pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/3 "2017-06-15T22:28:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [June 15, 2017, 10:49pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/4 "2017-06-15T22:49:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![thinkdffrnt](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@thinkdffrnt](https://discuss.elastic.co/u/thinkdffrnt)\
**Post date:** [June 15, 2017, 10:53pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/5 "2017-06-15T22:53:15Z")

</div>

I want to skip the calculation entirely

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [June 15, 2017, 11:16pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/6 "2017-06-15T23:16:09Z")

</div>

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

```auto
if(doc.containsKey('actual') && doc.containsKey('expected')) {
  // do calculation
}
else {
  // return default value
}

```

---

<div class="post-metadata">

**Author:** ![thinkdffrnt](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@thinkdffrnt](https://discuss.elastic.co/u/thinkdffrnt)\
**Post date:** [June 16, 2017, 2:04am UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/7 "2017-06-16T02:04:15Z")

</div>

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"}}}}}]}}

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [June 16, 2017, 2:09pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/8 "2017-06-16T14:09:24Z")

</div>

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:

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

```

---

<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:** [July 14, 2017, 2:09pm UTC](https://discuss.elastic.co/t/handling-empty-zero-values-in-scripted-fields/89600/9 "2017-07-14T14:09:35Z")

</div>

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