# Scripted fields not working for nested value in Kibana 7.x

**URL:** <https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647>\
**Category:** Kibana\
**Created:** [February 18, 2022, 8:35pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647 "2022-02-18T20:35:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankfenomena](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@frankfenomena](https://discuss.elastic.co/u/frankfenomena)\
**Post date:** [February 18, 2022, 8:35pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/1 "2022-02-18T20:35:42Z")

</div>

My scripted field works as intended in older version of kibana 5.x, but we use 7.x in a different environment and the same scripted field does not work. I've searched for what the syntax change would be in 7.x but couldn't find anything. Can anybody help me? Here is the scripted field statement. Note that the same statement below works fine when it's not a nested field.

```auto
try {
  return doc['notification.validation_result.keyword'].value;
} catch(Exception e) {
  return 'N/A';
}

```

---

<div class="post-metadata">

**Author:** ![Marta\_Bondyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marta_bondyra/32/102122_2.png) [@Marta\_Bondyra](https://discuss.elastic.co/u/Marta_Bondyra)\
**Post date:** [February 23, 2022, 8:21am UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/2 "2022-02-23T08:21:49Z")

</div>

Hi @frankfenomena, do you get any error message when trying to run it?

---

<div class="post-metadata">

**Author:** ![frankfenomena](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@frankfenomena](https://discuss.elastic.co/u/frankfenomena)\
**Post date:** [February 23, 2022, 2:38pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/3 "2022-02-23T14:38:38Z")

</div>

Hi @Marta_Bondyra, here is the exception message.

```auto
java.lang.IllegalStateException: 
A document doesnt have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"

```

---

<div class="post-metadata">

**Author:** ![Marta\_Bondyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marta_bondyra/32/102122_2.png) [@Marta\_Bondyra](https://discuss.elastic.co/u/Marta_Bondyra)\
**Post date:** [February 23, 2022, 9:48pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/4 "2022-02-23T21:48:43Z")

</div>

Thanks for clarifying. This is caused by a breaking change in 7.0 version - The doc['field'].value will throw an exception if the document is missing a value for the field.  
You can fix it by prepending:

```auto
if (doc['notification.validation_result.keyword'].size() == 0) return '';
try {
  return doc['notification.validation_result.keyword'].value;
} catch(Exception e) {
  return 'N/A';
}

```

---

<div class="post-metadata">

**Author:** ![frankfenomena](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@frankfenomena](https://discuss.elastic.co/u/frankfenomena)\
**Post date:** [February 23, 2022, 10:19pm UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/5 "2022-02-23T22:19:08Z")

</div>

Thanks Marta, I tried the above, but it doesn't return any values when there is a value for my field, and no exception message this time either.  
In your above example, do I need to put an else after the first line or is the else implied?

---

<div class="post-metadata">

**Author:** ![Marta\_Bondyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marta_bondyra/32/102122_2.png) [@Marta\_Bondyra](https://discuss.elastic.co/u/Marta_Bondyra)\
**Post date:** [February 24, 2022, 10:35am UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/6 "2022-02-24T10:35:09Z")

</div>

Hmm, that's strange. And does the field appear in discover or in the call to ES?  
You could try this one, but I am not sure it'll help:

```auto
if (doc['field'].size() == 1 && doc['field'] != null && doc.containsKey('field')) {      
      return doc['field'].value
}

```

---

<div class="post-metadata">

**Author:** ![frankfenomena](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@frankfenomena](https://discuss.elastic.co/u/frankfenomena)\
**Post date:** [February 25, 2022, 4:16am UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/7 "2022-02-25T04:16:24Z")

</div>

Yup the field appears in discover and is part of the process document and it shows a value, but not when I try to display it inside of a scripted field. I tried your suggestion above but I'm not getting a value to display.

---

<div class="post-metadata">

**Author:** ![frankfenomena](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@frankfenomena](https://discuss.elastic.co/u/frankfenomena)\
**Post date:** [March 9, 2022, 4:38am UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/8 "2022-03-09T04:38:50Z")

</div>

Hi @Marta_Bondyra , any other suggestions that I could try? Can you point me to the documentation about the breaking change in 7.0? All the links that people posted on other threads about the breaking change throw 404 errors.

---

<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 6, 2022, 4:39am UTC](https://discuss.elastic.co/t/scripted-fields-not-working-for-nested-value-in-kibana-7-x/297647/9 "2022-04-06T04:39:19Z")

</div>

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