# Subtraction between 2 different fields(with scripted fields)

**URL:** https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558
**Category:** Kibana
**Created:** [December 4, 2019, 3:18pm UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558 "2019-12-04T15:18:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)
#### Post date: [December 4, 2019, 3:18pm UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/1 "2019-12-04T15:18:55Z")

</div>

Hello!

I converted a string into int using scripted fields. Now I am trying to subtract this field with another field inside the the index pattern. My document looks something like this:

```auto
{
  "_source": {
   "type": "example",
   "example": {
    "value1": 000,
    "value_to_subtract": 6
    },
  "fields": {
    "test1": [
      000
    ],
    "test2_int_to_subtract": [
      8
    ],
    "timestamp": [
      "0000-00-00T00:00:00.000Z"
    ]
  }
}

```

So I am creating a scripted field like that  
` doc['test2_int_to_subtract'].value - doc['example.value_to_subtract'].value`,  
however I have shard failure in discover.

```auto
org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)
org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)
doc['test2_int_to_subtract'].value - doc['example.value_to_subtract'].value
    ^---- HERE

```

What am I doing wrong?  
Thank you very much in advance!

/Angelos

---

<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: [December 5, 2019, 11:02am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/2 "2019-12-05T11:02:55Z")

</div>

Hi & Welcome back!  
Could you try

`doc['fields.test2_int_to_subtract'].value - doc['example.value_to_subtract'].value`

Thanks!  
Matthias

---

<div class="post-metadata">

### Author: ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)
#### Post date: [December 5, 2019, 11:34am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/3 "2019-12-05T11:34:09Z")

</div>

Hi,  
Thank you!

I tried that but still have the same error with this caused reason:

```auto
No field found for [fields.test2_int_to_subtract] in mapping with types []

```

Is it because the fields are not under the \_source? Another thing that I am seeing is that under \_source the fields are inside `{}` but under fields the scripted field is inside `[]`.

Thank you in advance!

/Angelos

---

<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: [December 6, 2019, 6:52am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/4 "2019-12-06T06:52:43Z")

</div>

> [@Angelos](#):
>
> test2\_int\_to\_subtract"

So it’s not part of the \_source, how is ist set, could you share your indexpattern? Many thx!

---

<div class="post-metadata">

### Author: ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)
#### Post date: [December 6, 2019, 10:37am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/5 "2019-12-06T10:37:44Z")

</div>

When I create a scripted field and then see the document in Discover it puts it to `"fields":` (see first message)

```auto
{
  "_index": "test",
  "_type": "test",
  "_id": "test",
  "_version": 1,
  "_score": null,
  "_source": {
   "type": "example",
   "example": {
    "value1": 000,
    "value_to_subtract": 6
    },
  "fields": {
    "test1": [
      000
    ],
    "test2_int_to_subtract": [
      8
    ],
    "timestamp": [
      "0000-00-00T00:00:00.000Z"
    ]
  }
}
```

---

<div class="post-metadata">

### Author: ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)
#### Post date: [December 9, 2019, 7:17am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/6 "2019-12-09T07:17:55Z")

</div>

Basically I am trying to use(recalculate) an already existing scripted field.

---

<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: [December 9, 2019, 9:25am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/7 "2019-12-09T09:25:45Z")

</div>

Hi

You can't use the result of a scripted field in another scripted field. So you'd need to used to code of scripted field 1 in scripted field 2

`(code of test2_int_to_subtract) - doc['example.value_to_subtract'].value`

Best,  
Matthias

---

<div class="post-metadata">

### Author: ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)
#### Post date: [December 9, 2019, 11:05am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/8 "2019-12-09T11:05:40Z")

</div>

Hi,

Ok, thank you!

/Angelos

---

<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: [January 6, 2020, 11:05am UTC](https://discuss.elastic.co/t/subtraction-between-2-different-fields-with-scripted-fields/210558/9 "2020-01-06T11:05:45Z")

</div>

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