# Shard fails because of java.lang.IllegalStateException: A document doesn't have a value for a field!

**URL:** <https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440>\
**Category:** Kibana\
**Created:** [March 20, 2020, 3:25pm UTC](https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440 "2020-03-20T15:25:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leprovokateur](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@leprovokateur](https://discuss.elastic.co/u/leprovokateur)\
**Post date:** [March 20, 2020, 3:25pm UTC](https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440/1 "2020-03-20T15:25:14Z")

</div>

Hi,

I am using Elasticsearch and Kibana in version 7.6.1, which I migrated from 6.8.5  
I have a scripted field:

```auto
// user
String path = doc['logmessage.keyword'].value;
String logger = doc['logger.keyword'].value;
    if (path != null && path.toLowerCase().contains("user")) {
      if ("ClientSessionInitializer".equals(logger)) {
        int firstIndex = path.indexOf("'");
        int lastIndex = path.lastIndexOf("'");
        if (lastIndex > 0) {
            return path.substring(firstIndex+1,lastIndex);
          }
      } 
      if ("PermissionManager".equals(logger)) {
        int lastColon = path.lastIndexOf(":");
        if (lastColon > 0) {
            return path.substring(lastColon+1);
          }
      } 
       if ("LdapAuthenticationModule".equals(logger)) {
        int lastColon = path.lastIndexOf(":");
        if (lastColon > 0) {
            return path.substring(lastColon+1);
          }
      } 

    }
    return "";

```

which worked fine in verison 6.8, but now causes failing shards in Discovery:

```auto
[2020-03-20T16:18:10,603][DEBUG][o.e.a.s.TransportSearchAction] [Ada Lovelace] [531715] Failed to execute fetch phase
org.elasticsearch.transport.RemoteTransportException: [Ada Lovelace][6.204.132.58:9300][indices:data/read/search[phase/fetch/id]]
Caused by: org.elasticsearch.script.ScriptException: runtime error
        at org.elasticsearch.painless.PainlessScript.convertToScriptException(PainlessScript.java:94) ~[?:?]
        at org.elasticsearch.painless.PainlessScript$Script.execute(// user ...:837) ~[?:?]
        at org.elasticsearch.search.fetch.subphase.ScriptFieldsPhase.hitsExecute(ScriptFieldsPhase.java:67) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:178) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.SearchService.lambda$executeFetchPhase$5(SearchService.java:507) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:58) [elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:73) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:44) [elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:692) [elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-7.6.1.jar:7.6.1]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:830) [?:?]
Caused by: java.lang.IllegalStateException: A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!
        at org.elasticsearch.index.fielddata.ScriptDocValues$Strings.get(ScriptDocValues.java:496) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:503) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.painless.PainlessScript$Script.execute(// user ...:48) ~[?:?]
        at org.elasticsearch.search.fetch.subphase.ScriptFieldsPhase.hitsExecute(ScriptFieldsPhase.java:67) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:178) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.search.SearchService.lambda$executeFetchPhase$5(SearchService.java:507) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:58) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:73) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:44) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:692) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-7.6.1.jar:7.6.1]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[?:?]
        at java.lang.Thread.run(Thread.java:830) ~[?:?]

```

How would I debug this?

Best regards,  
Robert

---

<div class="post-metadata">

**Author:** ![lukeelmers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukeelmers/32/35230_2.png) [@lukeelmers](https://discuss.elastic.co/u/lukeelmers)\
**Post date:** [March 20, 2020, 11:14pm UTC](https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440/2 "2020-03-20T23:14:14Z")

</div>

> [@leprovokateur](#):
>
> A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!

Hi @leprovokateur,

Based on this error it sounds like maybe you need to add another check to make sure you don't hit a situation where `path` or `logger` may be empty (I see `path != null`, but is it possible `logger` is ever empty?)

Would you be able to provide a sample document to give a better idea as to the data you are working with?

---

<div class="post-metadata">

**Author:** ![leprovokateur](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@leprovokateur](https://discuss.elastic.co/u/leprovokateur)\
**Post date:** [March 25, 2020, 7:57am UTC](https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440/3 "2020-03-25T07:57:16Z")

</div>

Hi,

The documents differ quite often, but logger is always filled with a value.

Would it be possible to have the missing field printed in the stacktrace?

Best regards,  
Robert

---

<div class="post-metadata">

**Author:** ![leprovokateur](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@leprovokateur](https://discuss.elastic.co/u/leprovokateur)\
**Post date:** [March 30, 2020, 8:54am UTC](https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440/4 "2020-03-30T08:54:37Z")

</div>

Hi,

It's impossible to find a document, where the shard fails, as kibana doesn't show anything in this case and the elasticsearch log doesn't help either.

How would I find out, which field, index or document causes this error?  
What am I supposed to do, if the size of a field is 0? Return 0, "", or nothing at all?

Best regards,  
Robert

---

<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:** [April 27, 2020, 8:54am UTC](https://discuss.elastic.co/t/shard-fails-because-of-java-lang-illegalstateexception-a-document-doesnt-have-a-value-for-a-field/224440/5 "2020-04-27T08:54:47Z")

</div>

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