# How to create mapping that deals with variable types in fields

**URL:** <https://discuss.elastic.co/t/how-to-create-mapping-that-deals-with-variable-types-in-fields/11601>\
**Category:** Elasticsearch\
**Created:** [April 16, 2013, 5:34pm UTC](https://discuss.elastic.co/t/how-to-create-mapping-that-deals-with-variable-types-in-fields/11601 "2013-04-16T17:34:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_5/32/1873_2.png) [@Jason\_5](https://discuss.elastic.co/u/Jason_5)\
**Post date:** [April 16, 2013, 5:34pm UTC](https://discuss.elastic.co/t/how-to-create-mapping-that-deals-with-variable-types-in-fields/11601/1 "2013-04-16T17:34:57Z")

</div>

I have a situation in which the documents being indexed occasionally have  
fields with different types. I don't control the documents being indexed  
and sometimes they will have field that, when first seen, is a number type,  
but in later documents may be a string type.

I've tried to create a dynamic\_mapping to treat everything as a string, but  
it doesn't appear to be working for me.

The following sequence will fail (note the "test" field changes type):

curl -XDELETE "[http://localhost:9200/test/](http://localhost:9200/test/)"

curl -XPUT "[http://localhost:9200/test/](http://localhost:9200/test/)"

curl -XPUT [http://localhost:9200/test/test/\_mapping](http://localhost:9200/test/test/_mapping) -d '  
{  
"test": {  
"dynamic\_templates": [  
{  
"all\_fields": {  
"match": "\*",  
"match\_mapping\_type" : ["string", "integer", "long", "float",  
"double", "boolean"],  
"mapping": {  
"type": "string"  
}  
}  
}  
],  
"properties": {  
"meta": {  
"type": "object",  
"properties": {  
"timestamp": {  
"store": "yes",  
"type": "date"  
},  
"foo": {  
"type": "object",  
"properties": {  
"bar": {  
"index": "not\_analyzed",  
"type": "string"  
}  
}  
}  
}  
}  
}  
}  
}'

curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
{  
"meta": [{  
"timestamp": 1234567890,  
"test": 123,  
"foo": {  
"bar" : "Hello World"  
},  
"fruits" : {  
"canned": {  
"orchard" : ["Apples", "Oranges", "Peaches"]  
}  
}  
}]  
}'

curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
{  
"meta": [{  
"timestamp": 1234567890,  
"test": "abc",  
"foo": {  
"bar" : "Hello World"  
},  
"fruits" : {  
"canned": {  
"orchard" : ["Apples", "Oranges", "Peaches"]  
}  
}  
}]  
}'

The error reported is:

{  
"error": "MapperParsingException[Failed to parse [meta.test]]; nested:  
NumberFormatException[For input string: "abc"]; ",  
"status": 400  
}

Anyone know how I create a mapping to deal with this case?

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jason\_5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_5/32/1873_2.png) [@Jason\_5](https://discuss.elastic.co/u/Jason_5)\
**Post date:** [April 16, 2013, 6:55pm UTC](https://discuss.elastic.co/t/how-to-create-mapping-that-deals-with-variable-types-in-fields/11601/2 "2013-04-16T18:55:28Z")

</div>

Also the following fails despite the dynamic\_template in the mapping:

curl -XDELETE "[http://localhost:9200/test/](http://localhost:9200/test/)"

curl -XPUT "[http://localhost:9200/test/](http://localhost:9200/test/)"

curl -XPUT [http://localhost:9200/test/test/\_mapping](http://localhost:9200/test/test/_mapping) -d '  
{  
"test": {  
"date\_detection" : false,  
"numeric\_detection": false,  
"dynamic\_templates": [  
{  
"all\_fields": {  
"match": "\*",  
"match\_mapping\_type" : "integer",  
"mapping": {  
"type": "string"  
}  
}  
}  
]  
}  
}'

curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
{  
"test": 123,  
}'

curl -XPUT [http://localhost:9200/test/test/2](http://localhost:9200/test/test/2) -d '  
{  
"test": "abc",  
}'

Results in:

java.lang.NumberFormatException: For input string: "abc"  
at java.lang.NumberFormatException.forInputString(NumberFormatException.  
java:48)  
at java.lang.Long.parseLong(Long.java:410)  
at java.lang.Long.parseLong(Long.java:468)  
at org.elasticsearch.common.xcontent.support.AbstractXContentParser.  
longValue(AbstractXContentParser.java:72)  
at org.elasticsearch.index.mapper.core.LongFieldMapper.  
innerParseCreateField(LongFieldMapper.java:284)  
at org.elasticsearch.index.mapper.core.NumberFieldMapper.parseCreateField(  
NumberFieldMapper.java:182)  
at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(  
AbstractFieldMapper.java:307)  
... 11 more

On Tuesday, April 16, 2013 10:34:57 AM UTC-7, Jason wrote:

> I have a situation in which the documents being indexed occasionally have  
> fields with different types. I don't control the documents being indexed  
> and sometimes they will have field that, when first seen, is a number type,  
> but in later documents may be a string type.
> 
> I've tried to create a dynamic\_mapping to treat everything as a string,  
> but it doesn't appear to be working for me.
> 
> The following sequence will fail (note the "test" field changes type):
> 
> curl -XDELETE "[http://localhost:9200/test/](http://localhost:9200/test/)"
> 
> curl -XPUT "[http://localhost:9200/test/](http://localhost:9200/test/)"
> 
> curl -XPUT [http://localhost:9200/test/test/\_mapping](http://localhost:9200/test/test/_mapping) -d '  
> {  
> "test": {  
> "dynamic\_templates": [  
> {  
> "all\_fields": {  
> "match": "\*",  
> "match\_mapping\_type" : ["string", "integer", "long", "float",  
> "double", "boolean"],  
> "mapping": {  
> "type": "string"  
> }  
> }  
> }  
> ],  
> "properties": {  
> "meta": {  
> "type": "object",  
> "properties": {  
> "timestamp": {  
> "store": "yes",  
> "type": "date"  
> },  
> "foo": {  
> "type": "object",  
> "properties": {  
> "bar": {  
> "index": "not\_analyzed",  
> "type": "string"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
> {  
> "meta": [{  
> "timestamp": 1234567890,  
> "test": 123,  
> "foo": {  
> "bar" : "Hello World"  
> },  
> "fruits" : {  
> "canned": {  
> "orchard" : ["Apples", "Oranges", "Peaches"]  
> }  
> }  
> }]  
> }'
> 
> curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
> {  
> "meta": [{  
> "timestamp": 1234567890,  
> "test": "abc",  
> "foo": {  
> "bar" : "Hello World"  
> },  
> "fruits" : {  
> "canned": {  
> "orchard" : ["Apples", "Oranges", "Peaches"]  
> }  
> }  
> }]  
> }'
> 
> The error reported is:
> 
> {  
> "error": "MapperParsingException[Failed to parse [meta.test]];  
> nested: NumberFormatException[For input string: "abc"]; ",  
> "status": 400  
> }
> 
> Anyone know how I create a mapping to deal with this case?
> 
> Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jason\_5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_5/32/1873_2.png) [@Jason\_5](https://discuss.elastic.co/u/Jason_5)\
**Post date:** [April 16, 2013, 6:57pm UTC](https://discuss.elastic.co/t/how-to-create-mapping-that-deals-with-variable-types-in-fields/11601/3 "2013-04-16T18:57:24Z")

</div>

My bad.. the last post does work.. the mapping needs to be type _long_:

curl -XPUT [http://localhost:9200/test/test/\_mapping](http://localhost:9200/test/test/_mapping) -d '  
{  
"test": {  
"date\_detection" : false,  
"numeric\_detection": false,  
"dynamic\_templates": [  
{  
"all\_fields": {  
"match": "\*",  
"match\_mapping\_type" : "long",  
"mapping": {  
"type": "string"  
}  
}  
}  
]  
}  
}'

On Tuesday, April 16, 2013 10:34:57 AM UTC-7, Jason wrote:

> I have a situation in which the documents being indexed occasionally have  
> fields with different types. I don't control the documents being indexed  
> and sometimes they will have field that, when first seen, is a number type,  
> but in later documents may be a string type.
> 
> I've tried to create a dynamic\_mapping to treat everything as a string,  
> but it doesn't appear to be working for me.
> 
> The following sequence will fail (note the "test" field changes type):
> 
> curl -XDELETE "[http://localhost:9200/test/](http://localhost:9200/test/)"
> 
> curl -XPUT "[http://localhost:9200/test/](http://localhost:9200/test/)"
> 
> curl -XPUT [http://localhost:9200/test/test/\_mapping](http://localhost:9200/test/test/_mapping) -d '  
> {  
> "test": {  
> "dynamic\_templates": [  
> {  
> "all\_fields": {  
> "match": "\*",  
> "match\_mapping\_type" : ["string", "integer", "long", "float",  
> "double", "boolean"],  
> "mapping": {  
> "type": "string"  
> }  
> }  
> }  
> ],  
> "properties": {  
> "meta": {  
> "type": "object",  
> "properties": {  
> "timestamp": {  
> "store": "yes",  
> "type": "date"  
> },  
> "foo": {  
> "type": "object",  
> "properties": {  
> "bar": {  
> "index": "not\_analyzed",  
> "type": "string"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
> {  
> "meta": [{  
> "timestamp": 1234567890,  
> "test": 123,  
> "foo": {  
> "bar" : "Hello World"  
> },  
> "fruits" : {  
> "canned": {  
> "orchard" : ["Apples", "Oranges", "Peaches"]  
> }  
> }  
> }]  
> }'
> 
> curl -XPUT [http://localhost:9200/test/test/1](http://localhost:9200/test/test/1) -d '  
> {  
> "meta": [{  
> "timestamp": 1234567890,  
> "test": "abc",  
> "foo": {  
> "bar" : "Hello World"  
> },  
> "fruits" : {  
> "canned": {  
> "orchard" : ["Apples", "Oranges", "Peaches"]  
> }  
> }  
> }]  
> }'
> 
> The error reported is:
> 
> {  
> "error": "MapperParsingException[Failed to parse [meta.test]];  
> nested: NumberFormatException[For input string: "abc"]; ",  
> "status": 400  
> }
> 
> Anyone know how I create a mapping to deal with this case?
> 
> Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:40am UTC](https://discuss.elastic.co/t/how-to-create-mapping-that-deals-with-variable-types-in-fields/11601/4 "2017-07-06T02:40:52Z")

</div>


