# Auto detection of Data type

**URL:** <https://discuss.elastic.co/t/auto-detection-of-data-type/252571>\
**Category:** Elasticsearch\
**Created:** [October 19, 2020, 3:16pm UTC](https://discuss.elastic.co/t/auto-detection-of-data-type/252571 "2020-10-19T15:16:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdeshmukh](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@rdeshmukh](https://discuss.elastic.co/u/rdeshmukh)\
**Post date:** [October 19, 2020, 3:16pm UTC](https://discuss.elastic.co/t/auto-detection-of-data-type/252571/1 "2020-10-19T15:16:30Z")

</div>

This is the mapping initially which I created, considering that Roll number will be limit to only numbers. But eventually as they are growing in the roll numbers are Alphanumeric and the data type is changed.  
PUT student\_info  
{  
"mappings":  
{  
"properties":  
{  
"Roll\_no":{"type":"integer"},  
"Percentage": {"type": "float"},  
"Rank": {"type": "integer"},  
"Name":{"type":"text"}  
}  
}  
}

This the 1st record I want to insert to the index student\_info  
This is getting indexed correctly  
PUT student\_info/\_doc/1  
{  
"Roll\_no":123,  
"Percentage": 81.0,  
"Rank":4,  
"Name":"Sam"  
}

This is giving error  
PUT student\_info/\_doc/2  
{  
"Roll\_no":"A123",  
"Percentage": 83.0,  
"Rank":9,  
"Name":"David"  
}

Error "reason": "failed to parse field [Roll\_no] of type [integer] in document with id '2'. Preview of field's value: 'A123'"

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [October 20, 2020, 4:11am UTC](https://discuss.elastic.co/t/auto-detection-of-data-type/252571/2 "2020-10-20T04:11:50Z")

</div>

What is the question?  
If the mapping is number, other values will fail.  
You need to change the mapping to type text.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 20, 2020, 6:02am UTC](https://discuss.elastic.co/t/auto-detection-of-data-type/252571/3 "2020-10-20T06:02:13Z")

</div>

If you are using dynamic mapping the first occurrence of a field will determine the mapping. As you have the `Roll_no` set to a number this is how it is getting mapped. As mentioned this will prevent documents with non-numeric values in that field from getting indexed as mappings can not be changed for existing indices and each field has to have a single mapping. If you know a field can have non-numeric values, format it as a string in the JSON document and it will get mapped as a string. You can also specify the mapping ahead of time through an index template.

---

<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:** [November 17, 2020, 6:02am UTC](https://discuss.elastic.co/t/auto-detection-of-data-type/252571/4 "2020-11-17T06:02:16Z")

</div>

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