# Integer field behaving as string

**URL:** <https://discuss.elastic.co/t/integer-field-behaving-as-string/259280>\
**Category:** Elasticsearch\
**Created:** [December 21, 2020, 3:54pm UTC](https://discuss.elastic.co/t/integer-field-behaving-as-string/259280 "2020-12-21T15:54:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wissam](https://avatars.discourse-cdn.com/v4/letter/w/cab0a1/32.png) [@wissam](https://discuss.elastic.co/u/wissam)\
**Post date:** [December 21, 2020, 3:54pm UTC](https://discuss.elastic.co/t/integer-field-behaving-as-string/259280/1 "2020-12-21T15:54:34Z")

</div>

Hi,  
I'm facing a weird problem with field types in Elasticsearch (7.5.3).

I have a field of type "integer" (explicitly mapped), but every time I try to update it via a painless script by doing `ctx._source.myfield += 1`, a '1' is concatenated to the field instead of incrementing it by 1, and after multiple updates I'm getting this error:

[400] [mapper\_parsing\_exception] failed to parse field [myfield] of type [integer] in document with id 'xxxx'. Preview of field's value: '8311111111'

I looked up this record and found that the current value of myfield=831111111, so it's trying to concatenate another '1' and projecting the new value to become '8311111111' which doesn't fit the integer type

I did a workaround for now by changing the script to do the following, and it worked:  
`if (ctx._source.myfield instanceof String) { ctx._source.myfield = Integer.parseInt(ctx._source.myfield)+1 } else {ctx._source.myfield += 1 }`

But I would like to know why/how did this happen?

Please also note that this is not happening with all the records in the index.

Thx

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 21, 2020, 4:06pm UTC](https://discuss.elastic.co/t/integer-field-behaving-as-string/259280/2 "2020-12-21T16:06:05Z")

</div>

I can't find `+=` in the [list of operators](https://www.elastic.co/guide/en/elasticsearch/painless/7.5/painless-operators.html) that painless uses (even though it worked on my test I just did). Which makes this even more confusing. I'd think it would throw an error for using it.

Have you just tried `ctx._source.myfield ++` or `ctx._source.myfield + 1` which should increment the number by 1?

---

<div class="post-metadata">

**Author:** ![wissam](https://avatars.discourse-cdn.com/v4/letter/w/cab0a1/32.png) [@wissam](https://discuss.elastic.co/u/wissam)\
**Post date:** [December 22, 2020, 9:08am UTC](https://discuss.elastic.co/t/integer-field-behaving-as-string/259280/3 "2020-12-22T09:08:33Z")

</div>

Yes I tried both `ctx._source.myfield ++` and `ctx._source.myfield + 1` with the same result.

However I did some more testing to see when this is happening and when not, and found the following:

- as I mentioned before, `myfield` is already explicitly mapped as `integer`

- when I insert a new record with `myfield` as a `string` (`put test/_doc/1 {"myfield":"42"}}`) , it is saved normally without any errors.

- now when I update the above record (`post test/_update/1 {"script":{"source":"ctx._source.myfield ++"}}`) a `1` is concatenated and `myfield` becomes `421`

- when I insert a new record with `myfield` as an `integer` (`put test/_doc/2 {"myfield":42}}`) everything works fine

So it looks like when a `string` value is inserted in a field of type `integer`, it's being internally treated as a `string` in some functions and as an `integer` in others?

---

<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 19, 2021, 9:08am UTC](https://discuss.elastic.co/t/integer-field-behaving-as-string/259280/4 "2021-01-19T09:08:36Z")

</div>

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