# Scripted fields and fields not exists

**URL:** <https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660>\
**Category:** Kibana\
**Created:** [April 19, 2018, 9:09am UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660 "2018-04-19T09:09:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@marie](https://discuss.elastic.co/u/marie)\
**Post date:** [April 19, 2018, 9:09am UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/1 "2018-04-19T09:09:01Z")

</div>

Hi, I want in kibana created a scripted field to know a time of resolution of a project.

For that I writed this :

> Blockquote

doc['fields.LastDate'].date.dayOfYear- doc['fields.Bugdate'].date.dayOfYear

> Blockquote

And this functionned perfectly but I have lines In my index without this fields.

And I have an error like that:  
 ![5](https://us1.discourse-cdn.com/elastic/original/3X/0/9/09641bc5cd8e7425c541e677c9d33fe45e009624.PNG)

And it says it's because some lines don't have this 2 fields for created this time of resolution.  
And I can't see some lines in discover by this error.

For that I deleted my scripted field and like that I can see this other lines.  
Now I want to recreated this field but I can't see everything lines and I have this explanation:  
 ![6](https://us1.discourse-cdn.com/elastic/original/3X/3/a/3a2f4a4baea34d3057011f1ac5fda5f38da8eb38.PNG)

So I wanted for solved my first problem created an expression like that

> Blockquote

(doc['fields.LastDate'].exists && doc['fields.Bugdate'].exists ) ? doc['fields.LastDate'].date.dayOfYear- doc['fields.Bugdate'].date.dayOfYear : 0

> Blockquote

But this haven't functionned by le .exists do you have a means for created a field who say if the fields exists then do my first expression and if it not exists then return 0 ?  
And can the resolution of the first problem solve the second?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)\
**Post date:** [April 19, 2018, 10:45am UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/2 "2018-04-19T10:45:00Z")

</div>

Hey @marie,  
You get this expression because it´s a scripted field, scripted fields are not in the source-data and this says you that you can visualize and search on it, but you will not find this in your ES ( because its in Kibana).

You can write the following script to check if value is null

> **Script**
>
> ```
> if(doc['myvalue.keyword']).value == null){
> return 0;
> }else{
> ...Write your expression/painless script here here...
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@marie](https://discuss.elastic.co/u/marie)\
**Post date:** [April 19, 2018, 1:25pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/3 "2018-04-19T13:25:21Z")

</div>

I have do the test but I still have 0 lines to display in discover and I have again this :

![6](https://us1.discourse-cdn.com/elastic/original/3X/3/a/3a2f4a4baea34d3057011f1ac5fda5f38da8eb38.PNG) ![5](https://us1.discourse-cdn.com/elastic/original/3X/0/9/09641bc5cd8e7425c541e677c9d33fe45e009624.PNG)

---

<div class="post-metadata">

**Author:** ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)\
**Post date:** [April 19, 2018, 1:37pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/4 "2018-04-19T13:37:34Z")

</div>

I would mean you have some messages where your field is not present.

Can you post your Script here, perhaps there is a typing failure?

---

<div class="post-metadata">

**Author:** ![marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@marie](https://discuss.elastic.co/u/marie)\
**Post date:** [April 19, 2018, 1:56pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/5 "2018-04-19T13:56:47Z")

</div>

I have test like that

if(doc['fields.LastDate'].value == null && doc['fields.Bugdate'].value==null){  
return 0;  
}else{  
return doc['fields.LastDate'].date.dayOfYear- doc['fields.Bugdate'].date.dayOfYear;  
}

and like that

(doc['fields.Lastdate'].value==null && doc['fields.Bugdate']==null)?0:doc['fields.LastDate'].date.dayOfYear- doc['fields.Bugdate'].date.dayOfYear

and the both does the same

---

<div class="post-metadata">

**Author:** ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)\
**Post date:** [April 19, 2018, 2:20pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/6 "2018-04-19T14:20:40Z")

</div>

Hmmm ok... What version have your ELK Stack ?

---

<div class="post-metadata">

**Author:** ![marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@marie](https://discuss.elastic.co/u/marie)\
**Post date:** [April 19, 2018, 2:48pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/7 "2018-04-19T14:48:29Z")

</div>

I have 6.1.2 version

---

<div class="post-metadata">

**Author:** ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)\
**Post date:** [April 19, 2018, 4:56pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/8 "2018-04-19T16:56:33Z")

</div>

So test around your scripts in the console, perhaps you get there the errors that helps you to find your failures inside your scripts.

---

<div class="post-metadata">

**Author:** ![MimmiXYZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mimmixyz/32/30206_2.png) [@MimmiXYZ](https://discuss.elastic.co/u/MimmiXYZ)\
**Post date:** [April 19, 2018, 6:43pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/9 "2018-04-19T18:43:36Z")

</div>

I had this problem, too. The problem was that some of the fields that I used to calculate the scripted field had text which caused the script to fail. I soved it by calculating the value and adding it as a new field to the database, not as scripted field.

---

<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:** [May 17, 2018, 6:47pm UTC](https://discuss.elastic.co/t/scripted-fields-and-fields-not-exists/128660/10 "2018-05-17T18:47:34Z")

</div>

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