# Error in Discovery and vizualize when creating a script field

**URL:** <https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [November 9, 2020, 7:30pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818 "2020-11-09T19:30:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JinLauter](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JinLauter](https://discuss.elastic.co/u/JinLauter)\
**Post date:** [November 9, 2020, 7:30pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/1 "2020-11-09T19:30:00Z")

</div>

Hi,

We created a simple string script field, and we are getting a error in discovery and when we try to use that script field in any vizualization.  
I belive the images will explain it better:  
The script field:

 ![script](https://us1.discourse-cdn.com/elastic/original/3X/5/e/5e6c77584ba129aafd3e90af8c00ecb583b3ef52.png)  
Using the "Get help with the sintax and preview the results" tool:  
 ![resultados](https://us1.discourse-cdn.com/elastic/original/3X/4/9/49b40e7c01d06f5b34036a82407d1e5141f489c9.png)  
Error in Discovery:  
 ![erro discovery](https://us1.discourse-cdn.com/elastic/original/3X/6/a/6a330ee9ea73c3d29b336e9da73d7cfef02546b6.png)  
Error trying to use the field in a pie Vizualize:  
 ![vizualize](https://us1.discourse-cdn.com/elastic/original/3X/2/c/2c1b713047c3233cb91800699ce81a51cdc73b53.png)

Does anyone knows what could be the problem?

Thank you!!!!!!!!!!!

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 9, 2020, 8:04pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/2 "2020-11-09T20:04:31Z")

</div>

Are there any null values for `osInfo.raw` in your entire DB?

Also the error you posted has `if(name.contains.....` but I don't see that in the scripted field. Looks like the scripted field it's trying to run is more than just the one line I see in the other image.

Attempt to refresh index pattern also.

---

<div class="post-metadata">

**Author:** ![JinLauter](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JinLauter](https://discuss.elastic.co/u/JinLauter)\
**Post date:** [November 9, 2020, 10:59pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/3 "2020-11-09T22:59:06Z")

</div>

There is a lot of nulls actually, should I add a Isnull or something like it?

Abaout the error that have "..name.contains ....." I sorry, I posted the wrong print, the right one is here:

 ![discovery](https://us1.discourse-cdn.com/elastic/original/3X/5/c/5ce939338ad61dfbd5584d7b1999b5532e1a825d.png)  
The other print was from another test.

Thanks!

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 9, 2020, 11:12pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/4 "2020-11-09T23:12:02Z")

</div>

Was expecting a null pointer exception if that was true but I would add some conditional in there.

If the value null then add an empty string or something. If it tries to add the value of something that doesn't exist you will get errors.

I'd try that first.

---

<div class="post-metadata">

**Author:** ![JinLauter](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JinLauter](https://discuss.elastic.co/u/JinLauter)\
**Post date:** [November 9, 2020, 11:29pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/5 "2020-11-09T23:29:29Z")

</div>

Ok, going to try that. I think thats probaly it, I tested in discovery and vizualize again, but with results from the last 15 minutes and from the last 24 hours, and it worked with no problems, but when I put to get values from the last 7 days, the exact error appears again. I don't really know if a .IsNull() method works in the script, so I'm going to search the best way to deal with nulls.

---

<div class="post-metadata">

**Author:** ![JinLauter](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JinLauter](https://discuss.elastic.co/u/JinLauter)\
**Post date:** [November 9, 2020, 11:46pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/6 "2020-11-09T23:46:47Z")

</div>

I guess it was the null values. Tryed to solve by adding a conditional in the start:  
**if(doc['osInfo.raw'].value == null) { return " "; } else { name = doc['osInfo.raw'].value }**  
And after that I put the logic, a simple one:  
**if(name.contains("Windows 10")) { return "Windows 10"; } else if (name.contains("Windows 7")) { return "Windows 7"; } return " ";**

But it didn't solved it!, it worked when I added a filter directly in the vizualize and discovery: **osInfo.raw: exists**  
Thanks for the help!!!!!!

---

<div class="post-metadata">

**Author:** ![JinLauter](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JinLauter](https://discuss.elastic.co/u/JinLauter)\
**Post date:** [November 10, 2020, 5:22pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/7 "2020-11-10T17:22:56Z")

</div>

Having to put the filter to avoid errors have some limitations when you try to use filters with the script field, because it only works with the osinfo.raw: exists filter, and it removes from the query some lines that don't have this information, but would be usefull. Is there a right or specific way to filter null values? I tryied using isNull() inside an if,to put a black value in case isnull() was true, but it didn't work.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 10, 2020, 6:06pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/8 "2020-11-10T18:06:39Z")

</div>

@JinLauter

Try something like

```auto
if (doc['message.keyword'].size()==0)
{
    return "field is missing"
} else {
    if (doc['message.keyword'].value == '')
    {
        return "empty field"
    } else {
        return "has data and good"
    }
}

```

---

<div class="post-metadata">

**Author:** ![JinLauter](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JinLauter](https://discuss.elastic.co/u/JinLauter)\
**Post date:** [December 4, 2020, 5:40pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/9 "2020-12-04T17:40:08Z")

</div>

That did it! Thanks!

---

<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 1, 2021, 5:40pm UTC](https://discuss.elastic.co/t/error-in-discovery-and-vizualize-when-creating-a-script-field/254818/10 "2021-01-01T17:40:17Z")

</div>

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