A fix for 'A document doesn't have a value for a field' doesn't work anymore

Hi. I have a scripted field below and when I'm trying to visualize it by the field, it throws a 400 error, "A document doesn't have a value for a field". I've tried suggestions on the forum but none of them work. Were there any changes that are changing the behavior of .size method? Otherwise, how to fix the error?

def p = doc['nginx.access.url.keyword'].value;

if (doc['nginx.access.url.keyword'].size() == 0) return '';

if (p != '/') {
  int lastSlashIndex = p.lastIndexOf('/');
    if (lastSlashIndex > 0) {
     return p.substring(0, lastSlashIndex);
 }
}

return "";

As mentioned here, the values will be extracted from a specific document field which is set using the field key for the aggregations. But I can't find any info about the field key. Could you please point me on the reference?

The solution is to use nested condition:

if (doc['nginx.access.url.keyword'].size() != 0) {
  if (p != '/') {
  int lastSlashIndex = p.lastIndexOf('/');
    if (lastSlashIndex > 0) {
    return p.substring(0, lastSlashIndex);
 }
}
return doc['nginx.access.url.keyword'].value;

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