# Using painless to alter field contents during a reindex...null pointer exception

**URL:** <https://discuss.elastic.co/t/using-painless-to-alter-field-contents-during-a-reindex-null-pointer-exception/140368>\
**Category:** Elasticsearch\
**Created:** [July 17, 2018, 4:45pm UTC](https://discuss.elastic.co/t/using-painless-to-alter-field-contents-during-a-reindex-null-pointer-exception/140368 "2018-07-17T16:45:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam.Reynolds](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Adam.Reynolds](https://discuss.elastic.co/u/Adam.Reynolds)\
**Post date:** [July 17, 2018, 4:45pm UTC](https://discuss.elastic.co/t/using-painless-to-alter-field-contents-during-a-reindex-null-pointer-exception/140368/1 "2018-07-17T16:45:50Z")

</div>

I'm updating the mappings in a colleagues index and they're trying to change a field from a string to an int. That much is fine but the field currently has data in such as "89%" so obviously the data won't go in.

I'm trying to use Painless in the reindex query to modify the data but am getting a null\_pointer\_exception.

My reindex query is as follows,

> POST \_reindex  
> {  
> "conflicts": "proceed",  
> "source": {  
> "index": "old\_index"  
> },  
> "dest": {  
> "index": "new\_index"  
> },  
> "script": {  
> "lang": "painless",  
> "source": "if (ctx.\_source.the\_score=="100 %") {ctx.\_source.the\_score = 100} else {ctx.\_source.the\_score = ctx.\_source.the\_score.substring(0,2)}"  
> }  
> }

The error that's being output is as follows,

> {  
> "error": {  
> "root\_cause": [  
> {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "ctx.\_source.the\_score = ctx.\_source.the\_score.substring(0,2)}",  
> " ^---- HERE"  
> ],  
> "script": "if (ctx.\_source.the\_score=="100 %") {ctx.\_source.the\_score = 100} else {ctx.\_source.the\_score = ctx.\_source.the\_score.substring(0,2)}",  
> "lang": "painless"  
> }  
> ],  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "ctx.\_source.the\_score = ctx.\_source.the\_score.substring(0,2)}",  
> " ^---- HERE"  
> ],  
> "script": "if (ctx.\_source.the\_score=="100 %") {ctx.\_source.the\_score = 100} else {ctx.\_source.the\_score = ctx.\_source.the\_score.substring(0,2)}",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "null\_pointer\_exception",  
> "reason": null  
> }  
> },  
> "status": 500  
> }

I'm using Elasticsearch 5.6.8

Am I doing something wrong...or is there just a much better way of achieving what I'm trying to do?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [July 17, 2018, 9:38pm UTC](https://discuss.elastic.co/t/using-painless-to-alter-field-contents-during-a-reindex-null-pointer-exception/140368/2 "2018-07-17T21:38:46Z")

</div>

You should see a full stacktrace in your logs. Can you add that here or link to a gist?

---

<div class="post-metadata">

**Author:** ![Adam.Reynolds](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Adam.Reynolds](https://discuss.elastic.co/u/Adam.Reynolds)\
**Post date:** [July 18, 2018, 10:42am UTC](https://discuss.elastic.co/t/using-painless-to-alter-field-contents-during-a-reindex-null-pointer-exception/140368/3 "2018-07-18T10:42:34Z")

</div>

I'm running this on Elastic Cloud. It doesn't seem to be logging anything when I see the error after running the query in Kibana.

---

<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:** [August 15, 2018, 10:42am UTC](https://discuss.elastic.co/t/using-painless-to-alter-field-contents-during-a-reindex-null-pointer-exception/140368/4 "2018-08-15T10:42:45Z")

</div>

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