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

**URL:** <https://discuss.elastic.co/t/a-fix-for-a-document-doesnt-have-a-value-for-a-field-doesnt-work-anymore/212730>\
**Category:** Kibana\
**Created:** [December 21, 2019, 9:28am UTC](https://discuss.elastic.co/t/a-fix-for-a-document-doesnt-have-a-value-for-a-field-doesnt-work-anymore/212730 "2019-12-21T09:28:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devopsio](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Devopsio](https://discuss.elastic.co/u/Devopsio)\
**Post date:** [December 21, 2019, 9:28am UTC](https://discuss.elastic.co/t/a-fix-for-a-document-doesnt-have-a-value-for-a-field-doesnt-work-anymore/212730/1 "2019-12-21T09:28:32Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations.html#_values_source), 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?

---

<div class="post-metadata">

**Author:** ![Devopsio](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Devopsio](https://discuss.elastic.co/u/Devopsio)\
**Post date:** [December 23, 2019, 1:46pm UTC](https://discuss.elastic.co/t/a-fix-for-a-document-doesnt-have-a-value-for-a-field-doesnt-work-anymore/212730/2 "2019-12-23T13:46:08Z")

</div>

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;
```

---

<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:** [January 20, 2020, 1:46pm UTC](https://discuss.elastic.co/t/a-fix-for-a-document-doesnt-have-a-value-for-a-field-doesnt-work-anymore/212730/3 "2020-01-20T13:46:09Z")

</div>

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