# Number format Exception For string type

**URL:** https://discuss.elastic.co/t/number-format-exception-for-string-type/59175
**Category:** Elasticsearch
**Created:** [August 29, 2016, 11:29am UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175 "2016-08-29T11:29:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![parithiban](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@parithiban](https://discuss.elastic.co/u/parithiban)
#### Post date: [August 29, 2016, 11:29am UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/1 "2016-08-29T11:29:22Z")

</div>

I have a mapping like this

"users": {  
"user\_profiles": {  
"properties": {  
"score" : {  
"type": "string"  
}  
}  
}  
}

I can get the score as integer as well as "NA" so i mapped the type as string but while posting data to the index i am getting Number Format Exception.

while checking my log file i am getting this errors:

**[2016-08-29 15:19:01] elasticlog.WARNING: Response ["{"error":{"root\_cause":[{"type":"mapper\_parsing\_exception","reason":"failed to parse [score]"}],"type":"mapper\_parsing\_exception","reason":"failed to parse [score]","caused\_by":{"type":"number\_format\_exception","reason":"For input string: \"NH\""}},"status":400}"] []**

---

<div class="post-metadata">

### Author: ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)
#### Post date: [August 30, 2016, 6:26am UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/2 "2016-08-30T06:26:48Z")

</div>

Hi @parithiban,

any chance that you have defined this mapping a little while after you have created the index? Because in that case, Elasticsearch has probably derived a mapping automatically (we call that ["dynamic mapping"](https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html)).

To me it looks like the field `score` is defined as numeric field as you can see from the error description.

In case this was just a playground index, delete it and [create the mapping when you create the index](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings).

If this is an index with production data that you cannot afford to wipe, you need to create a new index including the mapping that you want and use the [reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html) to index the data again in the new index.

Daniel

---

<div class="post-metadata">

### Author: ![parithiban](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@parithiban](https://discuss.elastic.co/u/parithiban)
#### Post date: [August 30, 2016, 10:02am UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/3 "2016-08-30T10:02:23Z")

</div>

Thanks for the reply @danielmitterdorfer .We did the migration by first running the mapping and then only indexing data. The score index was already in the mapping.

My issue is that the first 100 records contain the score as integer it got indexed successfully and then after that the score was "NA" at this time only i am getting issue

---

<div class="post-metadata">

### Author: ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)
#### Post date: [August 30, 2016, 10:07am UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/4 "2016-08-30T10:07:13Z")

</div>

Hi @parithiban,

ah, now I understand. However, it appears to me that your mapping is different than what you've shown above. You can check the mapping with `GET /YOUR_INDEX_NAME_HERE/_mapping`. Check the response for the `score` field. It will most likely be a numeric field.

Daniel

---

<div class="post-metadata">

### Author: ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)
#### Post date: [May 25, 2017, 5:01pm UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/5 "2017-05-25T17:01:09Z")

</div>

I have same issue recently. i have production index mapping where i defined "auid" as "string", then later on i found it should be "integer" so i created a new index with mapping "auid" as integer, then using reindex API to re-index data from old index to the newindex, but I am getting the exception below??

````auto
  "failures" : [
    {
      "index" : "newindex",
      "type" : "author",
      "id" : "123",
      "cause" : {
        "type" : "mapper_parsing_exception",
        "reason" : "failed to parse [auid]",
        "caused_by" : {
          "type" : "number_format_exception",
          "reason" : "For input string: \"1111\""
        }
      },
      "status" : 400
    },
	```
````

---

<div class="post-metadata">

### Author: ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)
#### Post date: [May 26, 2017, 6:03am UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/6 "2017-05-26T06:03:37Z")

</div>

Hi,

did you create the new index before running the reindex? Please follow the example below. This worked fine for me on Elasticsearch 2.4.5 (as your talking about "string" I assumed you are on 2.x; in 5.x "string" type has been split into "text" and "keyword").

Let's start fresh and delete all involved indices for this example:

```auto
DELETE /old_books
DELETE /new_books

```

Then create the old index and add a document to it.

```auto
PUT /old_books
{
   "mappings": {
      "author": {
         "properties": {
            "name": {
               "type": "string"
            },
            "auid": {
                "type": "string"
            }
         }
      }
   }
}

PUT /old_books/author/1
{
    "name": "Mark Twain",
    "auid": "42"
}

```

Now we realize we made a mistake and create the new index where we correct the mapping of the `auid` field to `integer`:

```auto
PUT /new_books
{
   "mappings": {
      "author": {
         "properties": {
            "name": {
               "type": "string"
            },
            "auid": {
                "type": "integer"
            }
         }
      }
   }
}

```

and let the reindex API do its job:

```auto
POST /_reindex
{
  "source": {
    "index": "old_books"
  },
  "dest": {
    "index": "new_books"
  }
}

```

This worked fine for me. Here is the response:

```auto
{
   "took": 56,
   "timed_out": false,
   "total": 1,
   "updated": 0,
   "created": 1,
   "batches": 1,
   "version_conflicts": 0,
   "noops": 0,
   "retries": 0,
   "throttled_millis": 0,
   "requests_per_second": "unlimited",
   "throttled_until_millis": 0,
   "failures": []
}

```

Note, although Elasticsearch indexes the field now as integer, it is still a string in `_source`:

```auto
GET /new_books/author/_search
{
    "query": {
        "match_all": {}
    }
}

```

shows:

```auto
{
  ...
      "hits": [
         {
            "_index": "new_books",
            "_type": "author",
            "_id": "1",
            "_score": 1,
            "_source": {
               "name": "Mark Twain",
               "auid": "42"
            }
         }
      ]
...

```

If you do not want that you can use an inline script to convert the source as well during reindexing. Remember that inline scripts are a security risk and that you should rather use stored scripts for this. If you still want to use inline scripts then you need to (temporarily) enable them by setting `script.inline: true` in `config/elasticsearch.yml` and restart your cluster:

```auto
POST /_reindex
{
  "source": {
    "index": "old_books"
  },
  "dest": {
    "index": "new_books"
  },
"script": {
    "inline": "ctx._source.auid = Integer.valueOf(ctx._source.auid)"
  }
}

```

This will significantly slow-down the reindexing process though. Also check [how you can reindex without downtime](https://www.elastic.co/guide/en/elasticsearch/guide/current/index-aliases.html).

Daniel

---

<div class="post-metadata">

### Author: ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)
#### Post date: [May 26, 2017, 8:03pm UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/7 "2017-05-26T20:03:13Z")

</div>

many thanks Danial for your thorough explanation.

actually am using AWS ES 5.1. i did not change string to text bc i did not get any exception. though i just changed all "string" fields to "text". i followed same instructions as follows, but still getting same error

1. old index "old\_books" mapping contains "auid" as "string"
2. create new index with new mapping by setting "auid" as "integer"
3. re-index

POST /\_reindex  
{  
"source": {  
"index": "old\_books"  
},  
"dest": {  
"index": "new\_books"  
}  
}

Result:

{  
"index" : "new\_books",  
"type" : "author",  
"id" : "123",  
"cause" : {  
"type" : "mapper\_parsing\_exception",  
"reason" : "failed to parse [audoc.auid]",  
"caused\_by" : {  
"type" : "number\_format\_exception",  
"reason" : "For input string: "1111""  
}  
},  
"status" : 400  
}  
]  
}

---

<div class="post-metadata">

### Author: ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)
#### Post date: [May 30, 2017, 11:00pm UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/8 "2017-05-30T23:00:40Z")

</div>

well, i figured out the issue was due to some "auid" has value \> max length of integer datatype "2^31-1". by changing datatype to long, reindex went through with no issues.

---

<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: [July 5, 2017, 9:59pm UTC](https://discuss.elastic.co/t/number-format-exception-for-string-type/59175/9 "2017-07-05T21:59:50Z")

</div>


