# How to check a doc field exist while creating scripted field using painless

**URL:** <https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355>\
**Category:** Kibana\
**Created:** [February 8, 2017, 10:03am UTC](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355 "2017-02-08T10:03:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kambiu](https://avatars.discourse-cdn.com/v4/letter/k/8dc957/32.png) [@kambiu](https://discuss.elastic.co/u/kambiu)\
**Post date:** [February 8, 2017, 10:03am UTC](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355/1 "2017-02-08T10:03:59Z")

</div>

```
if (doc.containsKey('fieldname') {
  return 0; 
}
else {
  return 1;
}

```

The above code return compile error in kibana and ES 5.2.0.  
What is the correct syntax of painless to check if a field exists?

My solution:  
After searching a while, find that

> doc['field\_name'].empty  
> A boolean indicating if the field has no values within the doc.

will return true if the field does not exist on the records.

Ref

> **[Lucene Expressions Language | Elasticsearch Guide \[5.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting-expression.html)**

---

<div class="post-metadata">

**Author:** ![ppisljar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppisljar/32/11588_2.png) [@ppisljar](https://discuss.elastic.co/u/ppisljar)\
**Post date:** [February 8, 2017, 4:38pm UTC](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355/2 "2017-02-08T16:38:05Z")

</div>

what about `if (doc['fieldname'])` ?

---

<div class="post-metadata">

**Author:** ![kambiu](https://avatars.discourse-cdn.com/v4/letter/k/8dc957/32.png) [@kambiu](https://discuss.elastic.co/u/kambiu)\
**Post date:** [February 9, 2017, 2:30am UTC](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355/3 "2017-02-09T02:30:35Z")

</div>

`if (doc['fieldname'])`

It return a warning "Courier Fetch: 5 of 5 shards failed." when I search \*. And no result return.

Update:  
The following error found in elasticsearch.log

> Caused by: java.lang.ClassCastException: org.elasticsearch.index.fielddata.ScriptDocValues$Strings cannot be cast to java.lang.Boolean

---

<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:** [March 9, 2017, 2:31am UTC](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355/4 "2017-03-09T02:31:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [May 26, 2017, 5:20pm UTC](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355/5 "2017-05-26T17:20:59Z")

</div>

Did you solve your issue? I just posted a response here that might help you;

> [@Fields aggregation or field formatter?](https://discuss.elastic.co/t/fields-aggregation-or-field-formatter/87023/2):
>
> Hi Chris, I think a scripted field might help. Here's an experiment I did. I put this data using Kibana dev console. Each doc has a name but in a different field; PUT /discuss/test/1 { "date" : "2017-05-26T00:01:00", "name" : "Lee" } PUT /discuss/test/2 { "date" : "2017-05-26T00:02:00", "firstName" : "Bob" } PUT /discuss/test/3 { "date" : "2017-05-26T00:02:30", "fullName" : "Bart Simpson" } It looks like this in Discover; Obviously I can already sea…
