# Ternary operator: returning default values for missing document values

**URL:** <https://discuss.elastic.co/t/ternary-operator-returning-default-values-for-missing-document-values/188019>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 28, 2019, 10:04am UTC](https://discuss.elastic.co/t/ternary-operator-returning-default-values-for-missing-document-values/188019 "2019-06-28T10:04:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![todvora](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/todvora/32/49000_2.png) [@todvora](https://discuss.elastic.co/u/todvora)\
**Post date:** [June 28, 2019, 10:04am UTC](https://discuss.elastic.co/t/ternary-operator-returning-default-values-for-missing-document-values/188019/1 "2019-06-28T10:04:03Z")

</div>

Hi,  
I have troubles to execute the following painless script (used inside a FunctionScoreQuery):

```
1200 * Math.pow((doc.containsKey(params.population_key) ? doc[params.population_key].value : 0), 0.05)

```

The params.population\_key is a string and the field is present only in some documents, it's mapping is defined as a dynamic template of type double.

The execution works but logs following error:

> returning default values for missing document values is deprecated. Set system property '-Des.scripting.exception\_for\_missing\_value=true' to make behaviour compatible with future major versions!

It seems to me that the true branch of the ternary expression is evaluated eagerly, maybe because some kind of type conversion or so? Could you please suggest a way how to rewrite the script so I don't get any warning?

Thank you!  
Tomas

---

<div class="post-metadata">

**Author:** ![tholu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tholu/32/49015_2.png) [@tholu](https://discuss.elastic.co/u/tholu)\
**Post date:** [July 5, 2019, 10:47am UTC](https://discuss.elastic.co/t/ternary-operator-returning-default-values-for-missing-document-values/188019/2 "2019-07-05T10:47:03Z")

</div>

I have the same problem. The painless language seems to be rather painful here. ☹

---

<div class="post-metadata">

**Author:** ![todvora](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/todvora/32/49000_2.png) [@todvora](https://discuss.elastic.co/u/todvora)\
**Post date:** [July 17, 2019, 11:43am UTC](https://discuss.elastic.co/t/ternary-operator-returning-default-values-for-missing-document-values/188019/3 "2019-07-17T11:43:33Z")

</div>

I've probably found the problem. Inside the script, the `doc` object is a prepared data structure in a tabular format, optimized for fast access. It's not the underlying source document, which we indexed.

So the `doc` most likely contains all keys known from the index mapping. Which means that `doc.containsKey('any_mapping_key')` evaluates to `true` and the first branch of the ternary operator is always triggered.

I solved the problem by using `doc[params.population_key].empty` condition instead. It makes sense to combine this condition with `doc.containsKey(params.population_key)` if there is a chance that the param will be something not known from mappings.

---

<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:** [August 14, 2019, 11:47am UTC](https://discuss.elastic.co/t/ternary-operator-returning-default-values-for-missing-document-values/188019/4 "2019-08-14T11:47:26Z")

</div>

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