# Different values when trying to use scripted field

**URL:** <https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344>\
**Category:** Kibana\
**Created:** [May 18, 2021, 8:28pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344 "2021-05-18T20:28:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Gomez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/francisco_gomez1/32/78301_2.png) [@Francisco\_Gomez1](https://discuss.elastic.co/u/Francisco_Gomez1)\
**Post date:** [May 18, 2021, 8:28pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344/1 "2021-05-18T20:28:46Z")

</div>

Hi, im trying to create a scripted field wich is the result of 2 different values. Thanks in advance

This is the value i got stored in my document i access it usen the get "index"/\_doc/"id"  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/e/ee98d21f435d70f6f75db9131c60b1b1646ad40e.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/4/e449453f1b16628d889658d12fcc502fda274575.png)

If i manually add this 2 values i get a total of: 351.53

But when i use the scripted field i get the following value:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/d/bdb4521c504bc11cce462ce3e499b85a1b3a68ef.png)

And i dont know why. This is my script

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/7/370971384da123a99e19cf7eb02f8a1f7994c2b4.png)

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

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [May 19, 2021, 12:06pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344/2 "2021-05-19T12:06:19Z")

</div>

Check the mapping of your index - I assume `rotativocap` is indexed as a non-floating point type (like `integer`). If that's the case the docvalues (which are used here to calculate the scripted field) will work with a rounded version of the value.

To fix the problem, change your mapping so `rotativocap` is a e.g. a double, then re-index your existing data.

---

<div class="post-metadata">

**Author:** ![Francisco\_Gomez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/francisco_gomez1/32/78301_2.png) [@Francisco\_Gomez1](https://discuss.elastic.co/u/Francisco_Gomez1)\
**Post date:** [May 19, 2021, 1:01pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344/3 "2021-05-19T13:01:59Z")

</div>

It happens something curious here, take a look at this:

This is my mapping for rotativocap:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/6/167f131b1ed063def02e2bfb9f78a7e07b4edd20.png)

And this is the value stored if i access with a search:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/7/277c91e447dcb8ab04d64daf6f5053dc864f7cca.png)

But if i access from the script with the:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/5/b535ce77c75f80b9f842834bad92013958bfee09.png)

This is the value i get for rotativocap:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/f/ffa0cd514c7f0e35705fbade00c75669635c023f.png)

And it get yet worse becouse this is the mapping for the other one:

I dont have access to the .37

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [May 19, 2021, 1:46pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344/4 "2021-05-19T13:46:32Z")

</div>

It seems like one screenshot is missing in your answer.

Yes, this behavior is expected. The value you are seeing in the search is the one from `_source` (the original document transmitted to Elasticsearch), but it's not what powers scripted fields. Scripted fields work with docvalues which are the indexed values based on the type in the mapping (in this case `long`, so `219.37` is rounded to `219`).

In my previous post I mentioned how you can fix this - you need to adjust your mapping. This should be done like this:

- Create new index with the corrected mapping
- Use the re-index api to reindex your data from the current index into the new one with the correct mapping
- Use the new index from now on (you can work with index aliases so you don't have to change the name everywhere)

---

<div class="post-metadata">

**Author:** ![Francisco\_Gomez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/francisco_gomez1/32/78301_2.png) [@Francisco\_Gomez1](https://discuss.elastic.co/u/Francisco_Gomez1)\
**Post date:** [May 19, 2021, 3:52pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344/5 "2021-05-19T15:52:38Z")

</div>

Thank you so much for your help

---

<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:** [June 16, 2021, 3:52pm UTC](https://discuss.elastic.co/t/different-values-when-trying-to-use-scripted-field/273344/6 "2021-06-16T15:52:42Z")

</div>

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