# Handling sparse data in scripts

**URL:** <https://discuss.elastic.co/t/handling-sparse-data-in-scripts/25821>\
**Category:** Elasticsearch\
**Created:** [July 17, 2015, 4:56pm UTC](https://discuss.elastic.co/t/handling-sparse-data-in-scripts/25821 "2015-07-17T16:56:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BradVido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bradvido/32/741_2.png) [@BradVido](https://discuss.elastic.co/u/BradVido)\
**Post date:** [July 17, 2015, 4:56pm UTC](https://discuss.elastic.co/t/handling-sparse-data-in-scripts/25821/1 "2015-07-17T16:56:17Z")

</div>

Is there any way to check existence of a field within a script, and return null/undefined instead of a value?

Let's say I have a simple groovy script that sums the value of two fields:

```
doc['field1'].value + doc['field2'].value

```

Then I run a Stats aggregation in ES and get the `average` of the script values.

My question is how to handle sparse data, especially when both `field1` and `field2` are missing. I understand the script would return `0` in this case, which is not correct. The value is actually `_missing_` and shouldn't be included in the average. Returning 0 throws the average way off.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 17, 2015, 5:10pm UTC](https://discuss.elastic.co/t/handling-sparse-data-in-scripts/25821/2 "2015-07-17T17:10:27Z")

</div>

Would a null check work for you? Maybe the elvis operator if this is groovy.

---

<div class="post-metadata">

**Author:** ![BradVido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bradvido/32/741_2.png) [@BradVido](https://discuss.elastic.co/u/BradVido)\
**Post date:** [July 17, 2015, 8:44pm UTC](https://discuss.elastic.co/t/handling-sparse-data-in-scripts/25821/3 "2015-07-17T20:44:50Z")

</div>

No, inside the script the value isn't null (it's 0) if it's missing from the document.  
I'm curious if I can return null from the script to indicate there is no value, but the problem is I don't really have a way to determine that because missing values are converted to zeroes when the script tries to access them.

---

<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 6, 2017, 12:00am UTC](https://discuss.elastic.co/t/handling-sparse-data-in-scripts/25821/4 "2017-07-06T00:00:44Z")

</div>


