# How to check Mapping field not have any value

**URL:** <https://discuss.elastic.co/t/how-to-check-mapping-field-not-have-any-value/326906>\
**Category:** Kibana\
**Created:** [March 3, 2023, 5:25am UTC](https://discuss.elastic.co/t/how-to-check-mapping-field-not-have-any-value/326906 "2023-03-03T05:25:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PappuSingh](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@PappuSingh](https://discuss.elastic.co/u/PappuSingh)\
**Post date:** [March 3, 2023, 5:25am UTC](https://discuss.elastic.co/t/how-to-check-mapping-field-not-have-any-value/326906/1 "2023-03-03T05:25:43Z")

</div>

Hi,  
I am trying to create a new field to calculate the duration, during the creation of the field I did not find any issue or error, but while refreshing the Discover page throwing the below error.

![image](https://us1.discourse-cdn.com/elastic/original/3X/5/b/5b7dd45e7ca77c596c257fc8b97ce155f55ab16e.png)

Please see the below script for reference.

String completedtime= doc['readyCreatedTime'].value.toString();

if(completedtime=='-'){

long datenow = new Date().getTime();

long datewas = doc['createdTime'].value.getMillis();

long milisecondnd=datenow - datewas;

long second=milisecondnd/1000;

long hours=second/86400;

emit(hours);

}

else{

long datenow = doc['createdTime'].value.getMillis();

long datewas = doc['createdTime'].value.getMillis();

long milisecondnd=datenow - datewas;

long second=milisecondnd/1000;

long hours=second/86400;

emit(hours);

}

Sample data image

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/1/c11ff34994bee74a8207509acead7286d77562c7.png)

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [March 3, 2023, 6:04am UTC](https://discuss.elastic.co/t/how-to-check-mapping-field-not-have-any-value/326906/2 "2023-03-03T06:04:08Z")

</div>

Hi @PappuSingh

Please format your code in the future ... you have to check if the field exists you can not get the `.value` on a field that does not exist

I thought we answered this here

> [@Subtraction between current date and doc created date](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/4):
>
> Do you want Days in Integer or Float? Field Type Double long datenow = new Date().getTime(); long datewas = doc['@timestamp'].value.getMillis(); double days = (datenow - datewas) / 86400000.0; emit (days); Field Type Long long datenow = new Date().getTime(); long datewas = doc['@timestamp'].value.getMillis(); long days = (datenow - datewas) / 86400000; emit (days);

If you just want to check do something like this

```auto
if(doc['readyCreatedTime'].empty) {
  emit(0);
  return;
}

long datenow = new Date().getTime();
long datewas = doc['readyCreatedTime'].value.getMillis();
long days = (datenow - datewas) / 86400000;
emit (days);

```

---

<div class="post-metadata">

**Author:** ![PappuSingh](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@PappuSingh](https://discuss.elastic.co/u/PappuSingh)\
**Post date:** [March 3, 2023, 7:39am UTC](https://discuss.elastic.co/t/how-to-check-mapping-field-not-have-any-value/326906/3 "2023-03-03T07:39:21Z")

</div>

Thank you !!  
Here I am checking the condition and calculating the hours.  
The below script is working for me

int i = doc['readyCreatedTime'].size();

if(i==0){

long datenow = new Date().getTime();

long datewas = doc['createdTime'].value.getMillis();

long milisecondnd=datenow - datewas;

long second=milisecondnd/1000;

long hours=second/3600;

emit(hours);

}

else{

long datenow = doc['readyCreatedTime'].value.getMillis();

long datewas = doc['createdTime'].value.getMillis();

long milisecondnd=datenow - datewas;

long second=milisecondnd/1000;

long hours=second/3600;

emit(hours);

}

---

<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:** [March 31, 2023, 7:40am UTC](https://discuss.elastic.co/t/how-to-check-mapping-field-not-have-any-value/326906/4 "2023-03-31T07:40:13Z")

</div>

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