# Cast exception when trying to fill old data through re-indexing

**URL:** <https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490>\
**Category:** Elasticsearch\
**Created:** [July 20, 2019, 7:48pm UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490 "2019-07-20T19:48:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajith\_konara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajith_konara/32/50115_2.png) [@Rajith\_konara](https://discuss.elastic.co/u/Rajith_konara)\
**Post date:** [July 20, 2019, 7:48pm UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/1 "2019-07-20T19:48:14Z")

</div>

Hi

I am try to fill a new field to my old data through re-indexing, but when i run below it's gives me below error. The status field is set to long from mapping. Please help me to resolve this.

```
 "caused_by": {
      "type": "class_cast_exception",
      "reason": "Cannot apply [>] operation to types [java.lang.String] and [java.lang.Integer]."
    }

    POST _reindex
{
  "source": {
    "index": "mapped_index_long"
  },
  "dest": {
    "index": "stage_mapped_index",
    "version_type": "external"
  },
   "script": {
    "lang": "painless",
    "source": "if (ctx._source.status >= 200 && ctx._source.status < 400) {\n ctx._source.stage= \"Success\";\n} else {\n ctx._source.stage=\"Failed\";\n}"                                                                          
  }
}

```

Thank you,  
Rajith

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [July 20, 2019, 8:54pm UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/2 "2019-07-20T20:54:00Z")

</div>

The `ctx._source` value provides access to the actual indexed JSON document. The fact that it may be using a `long` mapping for field `status` only means that the value of the `status` field will be parsed as long while _indexing_ but if that field carries a string then it will still remain as string value in the reindex script.

To fix that try parsing the `ctx._source.status` with `Integer.parseInt` static method prior to making the comparison.,

---

<div class="post-metadata">

**Author:** ![Rajith\_konara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajith_konara/32/50115_2.png) [@Rajith\_konara](https://discuss.elastic.co/u/Rajith_konara)\
**Post date:** [July 21, 2019, 3:54am UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/3 "2019-07-21T03:54:49Z")

</div>

Hi Thiago,

Thank you for your response. I tried to parse  
`int x = Integer.parseInt(ctx._source.status);`  
But now i'am getting  
`"reason": "Cannot cast java.lang.Integer to java.lang.String"`

Thank you.

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [July 21, 2019, 1:39pm UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/4 "2019-07-21T13:39:06Z")

</div>

So seems like you have documents in which `status` can be either string or integer. You need to write a script to consider both cases.

---

<div class="post-metadata">

**Author:** ![Rajith\_konara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajith_konara/32/50115_2.png) [@Rajith\_konara](https://discuss.elastic.co/u/Rajith_konara)\
**Post date:** [July 22, 2019, 4:55pm UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/5 "2019-07-22T16:55:06Z")

</div>

Hi Thiago,

Thank you your inputs, finally managed to write a script to insert data. I am posting it below, hope it helps someone.

```
POST _reindex
{
  "source": {
    "index": "mapped_index_long"
  },
  "dest": {
    "index": "stage_mapped_index",
    "version_type": "external"
  },
   "script": {
    "lang": "painless",  
    "source": """
      if (ctx._source.status != "null") {
        int x = Integer.parseInt(String.valueOf(ctx._source.status));
        if (x >= 200 && x < 400) 
        {ctx._source.stage= "Success";} 
        else 
        {ctx._source.stage="Failed";}  
     }
      """                                                             
  }
}
```

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [July 22, 2019, 6:26pm UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/6 "2019-07-22T18:26:47Z")

</div>

That is almost good. Just change the null check to `ctx._source.status != null` which is probably what you meant.

---

<div class="post-metadata">

**Author:** ![Rajith\_konara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajith_konara/32/50115_2.png) [@Rajith\_konara](https://discuss.elastic.co/u/Rajith_konara)\
**Post date:** [July 23, 2019, 4:03am UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/7 "2019-07-23T04:03:21Z")

</div>

Thanks thiago, now it's working.

---

<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 20, 2019, 4:03am UTC](https://discuss.elastic.co/t/cast-exception-when-trying-to-fill-old-data-through-re-indexing/191490/8 "2019-08-20T04:03:26Z")

</div>

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