# Enforcing types

**URL:** https://discuss.elastic.co/t/enforcing-types/19033
**Category:** Elasticsearch
**Created:** [August 1, 2014, 3:25pm UTC](https://discuss.elastic.co/t/enforcing-types/19033 "2014-08-01T15:25:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)
#### Post date: [August 1, 2014, 3:25pm UTC](https://discuss.elastic.co/t/enforcing-types/19033/1 "2014-08-01T15:25:49Z")

</div>

Hi,

This is something I have discovered is happening... if I have a "long" type  
I can still store numbers such as 2, "4" and 1.2

Then when I retrieve the type is not long but string and float;(

In other words in Java when I examine my values I can have 2 (type long) ,  
"4" (type string) , 1.2 (type float)

But if I want to enforce the type at the time of "data entry" can I enforce  
only longs are accepted ?

I mean if I try to index "this is a number" I will get a format exception.

So can I force an exception when I try to index "4" instead of 4?

Or better still can ES convert the "4" to 4 (type long)  
And the 1.2 converted to 1 (type long)

?

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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [August 1, 2014, 3:46pm UTC](https://discuss.elastic.co/t/enforcing-types/19033/2 "2014-08-01T15:46:11Z")

</div>

Did you try to define a mapping for your field?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 1 août 2014 à 17:25:53, [eunever32@gmail.com](mailto:eunever32@gmail.com) ([eunever32@gmail.com](mailto:eunever32@gmail.com)) a écrit:

Hi,

This is something I have discovered is happening... if I have a "long" type I can still store numbers such as 2, "4" and 1.2

Then when I retrieve the type is not long but string and float;(

In other words in Java when I examine my values I can have 2 (type long) , "4" (type string) , 1.2 (type float)

But if I want to enforce the type at the time of "data entry" can I enforce only longs are accepted ?

I mean if I try to index "this is a number" I will get a format exception.

So can I force an exception when I try to index "4" instead of 4?

Or better still can ES convert the "4" to 4 (type long)  
And the 1.2 converted to 1 (type long)

?

## 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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.53dbb643.109cf92e.11c52%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.53dbb643.109cf92e.11c52%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)
#### Post date: [August 1, 2014, 5:58pm UTC](https://discuss.elastic.co/t/enforcing-types/19033/3 "2014-08-01T17:58:54Z")

</div>

okay so when I don't have a mapping and I do a GET \_mapping it returns this:  
{  
"test1": {  
"mappings": {  
"type2": {  
"properties": {  
"my\_int": {  
"type": "long"  
}  
}

Is that not the same as having a mapping?

Then to check the theory I created a mapping as follows:  
PUT test2  
PUT test2/lll/\_mapping  
{  
"properties": {  
"my\_int": {  
"type": "long"  
}  
}  
}  
PUT test2/lll/1  
{  
"my\_int": "1"  
}  
PUT test2/lll/2  
{  
"my\_int": 2  
}  
PUT test2/lll/3  
{  
"my\_int": 1.2  
}  
GET test2/lll/\_search

And I get the same results. See _bolded_ below:  
{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 3,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "test2",  
"\_type": "lll",  
"\_id": "1",  
"\_score": 1,  
"\_source": {

- 

```
          "my_int": "1"*
      }
   },
   {
      "_index": "test2",
      "_type": "lll",
      "_id": "2",
      "_score": 1,
      "_source": {

```

- 

```
          "my_int": 2*
      }
   },
   {
      "_index": "test2",
      "_type": "lll",
      "_id": "3",
      "_score": 1,
      "_source": {

```

- 

```
          "my_int": 1.2*
      }
   }
]

```

}  
}

So you can see above there are three values: a string, an int and a float  
all stored in the long value ???

Thanks,

On Friday, August 1, 2014 4:46:27 PM UTC+1, David Pilato wrote:

> Did you try to define a mapping for your field?
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr  
> [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)
> 
> Le 1 août 2014 à 17:25:53, [eune...@gmail.com](mailto:eune...@gmail.com) \<javascript:\> (  
> [eune...@gmail.com](mailto:eune...@gmail.com) \<javascript:\>) a écrit:
> 
> Hi,
> 
> This is something I have discovered is happening... if I have a "long"  
> type I can still store numbers such as 2, "4" and 1.2
> 
> Then when I retrieve the type is not long but string and float;(
> 
> In other words in Java when I examine my values I can have 2 (type long) ,  
> "4" (type string) , 1.2 (type float)
> 
> But if I want to enforce the type at the time of "data entry" can I  
> enforce only longs are accepted ?
> 
> I mean if I try to index "this is a number" I will get a format exception.
> 
> So can I force an exception when I try to index "4" instead of 4?
> 
> Or better still can ES convert the "4" to 4 (type long)  
> And the 1.2 converted to 1 (type long)
> 
> ?
> 
> ## 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7d3f2397-ab2e-4ac3-bce5-1ba411f7adad%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7d3f2397-ab2e-4ac3-bce5-1ba411f7adad%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)
#### Post date: [August 1, 2014, 8:43pm UTC](https://discuss.elastic.co/t/enforcing-types/19033/4 "2014-08-01T20:43:02Z")

</div>

The problem happens with java and python api when the typechecking throws  
exceptions  
For example when we use java to read the values mentioned :

test2/lll/1 my\_int = 1 class java.lang.String

test2/lll/2 my\_int = 2 class java.lang.Integer

test2/lll/3 my\_int = 1.2 class java.lang.Double

_So you see the client code will end up doing all kinds of casting_

Has anyone got any experience of how to stamp out such problems ?

On Friday, August 1, 2014 6:58:54 PM UTC+1, [eune...@gmail.com](mailto:eune...@gmail.com) wrote:

> okay so when I don't have a mapping and I do a GET \_mapping it returns  
> this:  
> {  
> "test1": {  
> "mappings": {  
> "type2": {  
> "properties": {  
> "my\_int": {  
> "type": "long"  
> }  
> }
> 
> Is that not the same as having a mapping?
> 
> Then to check the theory I created a mapping as follows:  
> PUT test2  
> PUT test2/lll/\_mapping  
> {  
> "properties": {  
> "my\_int": {  
> "type": "long"  
> }  
> }  
> }  
> PUT test2/lll/1  
> {  
> "my\_int": "1"  
> }  
> PUT test2/lll/2  
> {  
> "my\_int": 2  
> }  
> PUT test2/lll/3  
> {  
> "my\_int": 1.2  
> }  
> GET test2/lll/\_search
> 
> And I get the same results. See _bolded_ below:  
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 3,  
> "max\_score": 1,  
> "hits": [  
> {  
> "\_index": "test2",  
> "\_type": "lll",  
> "\_id": "1",  
> "\_score": 1,  
> "\_source": {
> 
> - 
> 
> ```
> "my_int": "1"*
> }
> },
> {
> "_index": "test2",
> "_type": "lll",
> "_id": "2",
> "_score": 1,
> "_source": {
> 
> ```
> 
> - 
> 
> ```
> "my_int": 2*
> }
> },
> {
> "_index": "test2",
> "_type": "lll",
> "_id": "3",
> "_score": 1,
> "_source": {
> 
> ```
> 
> - 
> 
> ```
> "my_int": 1.2*
> }
> }
> ]
> 
> ```
> 
> }  
> }
> 
> So you can see above there are three values: a string, an int and a float  
> all stored in the long value ???
> 
> Thanks,
> 
> On Friday, August 1, 2014 4:46:27 PM UTC+1, David Pilato wrote:
> 
> > Did you try to define a mapping for your field?
> > 
> > --  
> > _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> > @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr  
> > [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)
> > 
> > Le 1 août 2014 à 17:25:53, [eune...@gmail.com](mailto:eune...@gmail.com) ([eune...@gmail.com](mailto:eune...@gmail.com)) a écrit:
> > 
> > Hi,
> > 
> > This is something I have discovered is happening... if I have a "long"  
> > type I can still store numbers such as 2, "4" and 1.2
> > 
> > Then when I retrieve the type is not long but string and float;(
> > 
> > In other words in Java when I examine my values I can have 2 (type long)  
> > , "4" (type string) , 1.2 (type float)
> > 
> > But if I want to enforce the type at the time of "data entry" can I  
> > enforce only longs are accepted ?
> > 
> > I mean if I try to index "this is a number" I will get a format exception.
> > 
> > So can I force an exception when I try to index "4" instead of 4?
> > 
> > Or better still can ES convert the "4" to 4 (type long)  
> > And the 1.2 converted to 1 (type long)
> > 
> > ?
> > 
> > ## 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/4e489d04-929e-45a2-9268-21b3d03c4e3f%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/735c401a-8356-4d6a-9e1e-fe96f629f48b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/735c401a-8356-4d6a-9e1e-fe96f629f48b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:11am UTC](https://discuss.elastic.co/t/enforcing-types/19033/5 "2017-07-06T01:11:31Z")

</div>


