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.
I can't find += in the list of operators 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?
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?
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.