When an index sometimes does- and sometimes doesn't contain a certain field that a scripted field is dependent on, Kibana's "Discover"-page can spit out "Shard failed"-errors.

To circumvent this, the scripted field can be made nullable.

Painless:

if (doc.containsKey('THE_FIELD') && doc['THE_FIELD'].size() > 0)  
{
    return doc['THE_FIELD'].value;
}

return null;

Or as a one-liner:

if (doc.containsKey('THE_FIELD') && doc['THE_FIELD'].size() > 0) { return doc['THE_FIELD'].value; } return null;