# Stutract Two values

**URL:** https://discuss.elastic.co/t/stutract-two-values/198869
**Category:** Elasticsearch
**Created:** [September 10, 2019, 10:17am UTC](https://discuss.elastic.co/t/stutract-two-values/198869 "2019-09-10T10:17:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Arpan.S](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Arpan.S](https://discuss.elastic.co/u/Arpan.S)
#### Post date: [September 10, 2019, 10:17am UTC](https://discuss.elastic.co/t/stutract-two-values/198869/1 "2019-09-10T10:17:37Z")

</div>

Hi,

I want to get difference between two values.

suppose I have data in following format.  
"\_source" : {  
"students" : [  
{  
"name" : "A",  
"total\_fee" : 12345,  
"paid\_fee" : 12344.8  
},  
{  
"name" : "B",  
"total\_fee" : 23456,  
"paid\_fee" : 23455.6  
}  
]  
}

Now I want to get the difference between "total\_fee" and "paid\_fee" for each student.  
I have already tried  
{  
"query": {  
"bool": {  
"filter": {  
"script": {  
"script": "doc['students.total\_fee'].value - doc['students.paid\_fee'].value"  
}  
}  
}  
}  
}  
but it is returning empty "hits".

I have also tried

{  
"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "students",  
"query": {  
"script": {  
"script": {  
"inline": "doc['students.total\_fee'].value - doc['students.paid\_fee'].value",  
"lang": "expression"  
}  
}  
}  
}  
}  
]  
}  
}

also the "script\_fields" did not worked.

Please suggest how can I achieve the desired output.

Thanks in advance 😃 .

---

<div class="post-metadata">

### Author: ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)
#### Post date: [September 10, 2019, 10:24am UTC](https://discuss.elastic.co/t/stutract-two-values/198869/2 "2019-09-10T10:24:45Z")

</div>

> [@Arpan.S](#):
>
> difference

You should use script field:

```
{
   "script_fields" : {
        "difference" : {
            "script" : "doc['total_fee'].value - doc['paid_fee'].value"
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![Arpan.S](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Arpan.S](https://discuss.elastic.co/u/Arpan.S)
#### Post date: [September 10, 2019, 10:31am UTC](https://discuss.elastic.co/t/stutract-two-values/198869/3 "2019-09-10T10:31:14Z")

</div>

Hi @wangqinghuan,

I tried "script\_fields" but it is resulting to "0" not the correct difference.  
Is there anything to do with nested data?

Thanks for quick response.

---

<div class="post-metadata">

### Author: ![Arpan.S](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Arpan.S](https://discuss.elastic.co/u/Arpan.S)
#### Post date: [September 10, 2019, 10:35am UTC](https://discuss.elastic.co/t/stutract-two-values/198869/4 "2019-09-10T10:35:41Z")

</div>

I have tried

> [@wangqinghuan](#):
>
> { "script\_fields" : { "difference" : { "script" : "doc['students.total\_fee'].value - doc['students.paid\_fee'].value" } } }

but it is giving 0 as difference.

Is it because difference is very less?

If I'm not using "students." it is giving "no field found [total\_fee]" error.

---

<div class="post-metadata">

### Author: ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)
#### Post date: [September 11, 2019, 1:08am UTC](https://discuss.elastic.co/t/stutract-two-values/198869/5 "2019-09-11T01:08:52Z")

</div>

> Is it because difference is very less?

No.

> If I'm not using "students." it is giving "no field found [total\_fee]" error.

students.total\_fee is a nested structure. Why do you use nested document?

---

<div class="post-metadata">

### Author: ![Arpan.S](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Arpan.S](https://discuss.elastic.co/u/Arpan.S)
#### Post date: [September 11, 2019, 4:53am UTC](https://discuss.elastic.co/t/stutract-two-values/198869/6 "2019-09-11T04:53:19Z")

</div>

> [@wangqinghuan](#):
>
> Why do you use nested document?

The data I have is in nested format. The student data was just for example.

---

<div class="post-metadata">

### Author: ![Arpan.S](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Arpan.S](https://discuss.elastic.co/u/Arpan.S)
#### Post date: [September 11, 2019, 12:25pm UTC](https://discuss.elastic.co/t/stutract-two-values/198869/7 "2019-09-11T12:25:37Z")

</div>

Hi,

Thanks for your replies.

I found the solution on [stackOverflow](https://stackoverflow.com/q/57869176/8640819).

---

<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: [October 9, 2019, 12:26pm UTC](https://discuss.elastic.co/t/stutract-two-values/198869/8 "2019-10-09T12:26:18Z")

</div>

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