# Calculated field not working

**URL:** <https://discuss.elastic.co/t/calculated-field-not-working/134863>\
**Category:** Elasticsearch\
**Created:** [June 6, 2018, 7:15pm UTC](https://discuss.elastic.co/t/calculated-field-not-working/134863 "2018-06-06T19:15:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zareh\_Vazquez](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@Zareh\_Vazquez](https://discuss.elastic.co/u/Zareh_Vazquez)\
**Post date:** [June 6, 2018, 7:15pm UTC](https://discuss.elastic.co/t/calculated-field-not-working/134863/1 "2018-06-06T19:15:10Z")

</div>

Hi all,

I am trying to create a simple calculated field by a simple subtraction of two fields within the same index. My query looks something like this:

> {  
> "\_source": {  
> "includes": ["realTimestamp", "clientDelay", "networkDelay"]  
> },  
> "query": {  
> "range": {  
> "realTimestamp": {  
> "gte": "06/05/2018 00:00:00",  
> "lt": "06/05/2018 23:59:59"  
> }  
> }  
> },  
> "script\_fields": {  
> "browserDelay": {  
> "script": {  
> "lang": "painless",  
> "source": "doc['clientDelay'].value - doc['networkDelay'].value;"  
> }  
> }  
> }  
> }

But the results looks like this:

> "\_source": {  
> "realTimestamp": "06/05/2018 23:59:43",  
> "networkDelay": 0.834,  
> "clientDelay": 1.058  
> },  
> "fields": {  
> "browserDelay": [  
> 1  
> ]  
> }

Seems that the output of the calculate is a simple integer rather that a decimal. I also tried the following but I got the same result.

> {  
> "\_source": {  
> "includes": ["realTimestamp", "clientDelay", "networkDelay"]  
> },  
> "query": {  
> "range": {  
> "realTimestamp": {  
> "gte": "06/05/2018 00:00:00",  
> "lt": "06/05/2018 23:59:59"  
> }  
> }  
> },  
> "script\_fields": {  
> "browserDelay": {  
> "script": {  
> "lang": "painless",  
> "source": "float browserDelay; browserDelay = doc['clientDelay'].value - doc['networkDelay'].value; return browserDelay;"  
> }  
> }  
> }  
> }

Any reason what seems the results to be round up?

> "\_source": {  
> "realTimestamp": "06/05/2018 23:59:40",  
> "networkDelay": 0.787,  
> "clientDelay": 2.161  
> },  
> "fields": {  
> "browserDelay": [  
> 2  
> ]  
> }

Thanks in advance,

Zareh

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [June 11, 2018, 3:46pm UTC](https://discuss.elastic.co/t/calculated-field-not-working/134863/2 "2018-06-11T15:46:34Z")

</div>

Hi Zareh,

This does seem strange. I'm curious as to what the values of clientDelay and networkDelay are within the script. Would you please try running the a couple of debugging scripts:

1. "Debug.explain(doc['clientDelay'].value);"
2. "Debug.explain(doc['networkDelay'].value);"

Thanks,  
Jack

---

<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:** [July 9, 2018, 3:46pm UTC](https://discuss.elastic.co/t/calculated-field-not-working/134863/3 "2018-07-09T15:46:38Z")

</div>

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