# Painless scripts: "else if" and ternary conditionals checking field existence don't work properly

**URL:** <https://discuss.elastic.co/t/painless-scripts-else-if-and-ternary-conditionals-checking-field-existence-dont-work-properly/200021>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 18, 2019, 1:54pm UTC](https://discuss.elastic.co/t/painless-scripts-else-if-and-ternary-conditionals-checking-field-existence-dont-work-properly/200021 "2019-09-18T13:54:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Doman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_doman/32/11409_2.png) [@Thomas\_Doman](https://discuss.elastic.co/u/Thomas_Doman)\
**Post date:** [September 18, 2019, 1:54pm UTC](https://discuss.elastic.co/t/painless-scripts-else-if-and-ternary-conditionals-checking-field-existence-dont-work-properly/200021/1 "2019-09-18T13:54:16Z")

</div>

ES: 6.1.1  
NEST: 6.1

As I've researched, I've tried two different methods of conditionally populating a "date" variable using both `containsKey` and `doc['field'].value != null` with the same outcome. I've used both "else if" and, having read somewhere that painless doesn't support "else if", I tried a ternary construct. The issue is, only the first conditional check seems to engage. Both methods not only compile but also _ **produce the same results** _ when used for filtering. Here are the two methods:  
1.

```auto
POST _scripts/filterImpliedPublishDateRange
{
 "script": {
  "lang": "painless",
  "source":
"""
double now = params['now'];
double from = params['from'], to = params['to'];
double impliedPublishDate = 0.0;
if (doc.containsKey('publishDate'))
{
 impliedPublishDate = doc['publishDate'].value.getMillis();
}
else if (doc.containsKey('dateCreated'))
{
 impliedPublishDate = doc['dateCreated'].value.getMillis();
}
else if (doc.containsKey('modifiedDate'))
{
 impliedPublishDate = doc['modifiedDate'].value.getMillis();
}
double days = (now - impliedPublishDate) / (3600000.0*24);
return ((days > from) && (days <= to));
"""
 }
}

```

1. 

```auto
POST _scripts/filterImpliedPublishDateRange
{
  "script": {
    "lang": "painless",
    "source":
"""
double now = params['now'];
double from = params['from'], to = params['to'];
double impliedPublishDate = doc['publishDate'].value != null ? doc['publishDate'].value.getMillis() : doc['dateCreated'].value != null ? doc['dateCreated'].value.getMillis() : doc['modifiedDate'].value != null ? doc['modifiedDate'].value.getMillis() : 0.0;
double days = (now - impliedPublishDate) / (3600000.0*24);
return ((days > from) && (days <= to));
"""
  }
}

```

Both of these approaches evidently only use whichever is the first field I specify, `publishDate`, `dateCreated`, etc. and it doesn't seem to fall back to the next conditional for documents which don't have the field I specify in the first `if`. There are many documents that have one of the fields I'm checking for. Whichever field I choose first _ **does** _ yield the correct number and range of documents. For some reason, it just doesn't consider the documents where the first conditional is `false`.

---

<div class="post-metadata">

**Author:** ![Thomas\_Doman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_doman/32/11409_2.png) [@Thomas\_Doman](https://discuss.elastic.co/u/Thomas_Doman)\
**Post date:** [September 18, 2019, 7:18pm UTC](https://discuss.elastic.co/t/painless-scripts-else-if-and-ternary-conditionals-checking-field-existence-dont-work-properly/200021/2 "2019-09-18T19:18:10Z")

</div>

So, a co-worker of mine discovered that what I needed was `!doc['publishDate'].empty`. How that differs from `containsKey` or `value != null` escapes me because I haven't found any comprehensive documentation any of those 3 expressions but I can say that the `empty` approach produces the result I expect.

Anyone have any pointers to painless documentation on these 3? I found [Lucene Expressions Language](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/modules-scripting-expression.html) but that still doesn't really distinguish the difference between checking the `value` for null and `empty`.

---

<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:** [October 16, 2019, 7:18pm UTC](https://discuss.elastic.co/t/painless-scripts-else-if-and-ternary-conditionals-checking-field-existence-dont-work-properly/200021/3 "2019-10-16T19:18:27Z")

</div>

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