# Scripted field

**URL:** https://discuss.elastic.co/t/scripted-field/267225
**Category:** Kibana
**Created:** [March 15, 2021, 7:05am UTC](https://discuss.elastic.co/t/scripted-field/267225 "2021-03-15T07:05:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Changra](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@Changra](https://discuss.elastic.co/u/Changra)
#### Post date: [March 15, 2021, 7:05am UTC](https://discuss.elastic.co/t/scripted-field/267225/1 "2021-03-15T07:05:18Z")

</div>

Hi ,

I am trying to substract to date format vales through scripted field in kibana scripted fileds

```
if(doc['response_time'] > doc['request_time']){
    return doc['response_time'].value.getMillis() - doc['request_time'].value.getMillis();
}

```

But it gives me the error ```  
Cannot apply [\>] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Dates] and [org.elasticsearch.index.fielddata.ScriptDocValues.Dates]

```auto

Regards,
Neha Changra
```

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [March 15, 2021, 9:47am UTC](https://discuss.elastic.co/t/scripted-field/267225/2 "2021-03-15T09:47:22Z")

</div>

Hi

You could try it the following way (can be further improved by using variables)

```
if(doc['response_time'].value.getMillis() > doc['request_time'].value.getMillis()){
    return doc['response_time'].value.getMillis() - doc['request_time'].value.getMillis();
}

```

Hope this helps,  
Best,  
Matthias

---

<div class="post-metadata">

### Author: ![Changra](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@Changra](https://discuss.elastic.co/u/Changra)
#### Post date: [April 7, 2021, 8:09am UTC](https://discuss.elastic.co/t/scripted-field/267225/3 "2021-04-07T08:09:46Z")

</div>

Hi Team,

I have been to substract the date value and store the value in the variable defined as " duration" and then check if the value is smaller than 2 then return a value through scripted filed in kibana

```
    if (doc['request_time'].size()!=0 && doc['response_time']!=0 && doc['response_time'].value.getMillis() > doc['request_time'].value.getMillis())
            {
        return doc['response_time_milli'].value.getMillis() - doc['request_time_milli'].value.getMillis()= doc['duration'].value.getMillis();
            }
               if (doc['duration'].value.getMillis() < 2)
               {
                return doc['duration_2'].value.getMillis() = 1;
               }

```

` although i get an error`

````
```
"script": "if (doc['request_time'].size()!=0 && doc['response_time']!=0 && doc['response_time'].value.getMillis() > doc['request_time'].value.getMillis())\n {\n return doc['response_time_milli'].value.getMillis() - doc['request_time_milli'].value.getMillis()= doc['duration'].value.getMillis();\n }\n if (doc['duration'].value.getMillis() < 2)\n {\n return doc['duration_2'].value.getMillis() = 1;\n }\n ",
"lang": "painless",
"caused_by": {
 "type": "illegal_argument_exception",
 "reason": "Left-hand side cannot be assigned a value."
}

````

```auto

    Regards,
Neha Changra
```

---

<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 5, 2021, 8:09am UTC](https://discuss.elastic.co/t/scripted-field/267225/4 "2021-05-05T08:09:50Z")

</div>

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