# Long field is returned as integer if the value is less in Java API

**URL:** https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127
**Category:** Elasticsearch
**Created:** [September 25, 2012, 6:24am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127 "2012-09-25T06:24:03Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![Peter\_Briggs](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@Peter\_Briggs](https://discuss.elastic.co/u/Peter_Briggs)
#### Post date: [November 29, 2013, 11:07am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/10 "2013-11-29T11:07:22Z")

</div>

For anyone else who encounters this issue, there is some discussion of what  
causes this behaviour here:  
[https://groups.google.com/forum/#!searchin/elasticsearch/getsource$20integer$20long/elasticsearch/jxIY22TmA8U/PyqZPPyYQ0gJ](https://groups.google.com/forum/#!searchin/elasticsearch/getsource$20integer$20long/elasticsearch/jxIY22TmA8U/PyqZPPyYQ0gJ)

On Tuesday, 25 September 2012 11:51:43 UTC+1, Derry O' Sullivan wrote:

> Looks like mappings are only used for ingestion:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)
> 
> According to the search docs:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/)
> 
> Number Values
> 
> All REST APIs support providing numbered parameters as string on top of  
> supporting the native JSON number types.  
> I'm not sure how ES marshalls JSON but that may be where the problem is -  
> it's just returning the number based on the data.
> 
> On 25 September 2012 11:39, Derry O' Sullivan \<[der...@gmail.com](mailto:der...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > On 25 September 2012 11:37, Deepak Chezhian \<[cdeep...@gmail.com](mailto:cdeep...@gmail.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > Hi Derry,
> > > 
> > > I am taking the value Long object which is initialized as "1L" so there  
> > > might not be any problem with that, even i tried explicit conversion to  
> > > Long which setting the value to the field.
> > > 
> > > Well i'm stumped then. Sounds like you are inserting a long into a field  
> > > that should be a long and getting back to int. Only think i can think of is  
> > > that ES is automatically setting the return type based on the value but i'm  
> > > unsure as to why it would do that over mappings....
> > 
> > Regards,
> > 
> > > Deepak
> > > 
> > > On Tuesday, 25 September 2012 14:30:17 UTC+4, Derry O' Sullivan wrote:
> > > 
> > > > On 25 September 2012 11:26, Deepak Chezhian [cdeep...@gmail.com](mailto:cdeep...@gmail.com) wrote:
> > > > 
> > > > > Hi Derry,
> > > > > 
> > > > > PFB.
> > > > > 
> > > > > _Are you getting this error when doing a get or a search?_  
> > > > > Search.
> > > > > 
> > > > > Also, how are you setting the value of 1 vs 999999999999999999?  
> > > > > 1 is set programmatically using java api(indexing a document). 999999999999999999  
> > > > > is set using manually by using REST apis. I tried to set some integer  
> > > > > values with REST api but it is still coming as integer only, only when i  
> > > > > set values above the limit of the integer data type then long type is  
> > > > > returned.
> > > > 
> > > > Are you indexing 1 or 1L in java?  
> > > > [Java's L number (long) specification - Stack Overflow](http://stackoverflow.com/questions/769963/javas-l-)  
> > > > number-long-specification-question
> > > > 
> > > > I suspect adding an L to the end of the number may solve the problem if  
> > > > doing via the java api. If you don't specify that or have the number  
> > > > cast/typed as a long in advance, it probably will add as an int. Surprising  
> > > > that ES is not casting to the type specified on the mapping though...
> > > > 
> > > > > \*If retrieving via the api, it would be interesting to see what  
> > > > > .getSource().get("store\_id").getClass().getName()? \*
> > > > > 
> > > > > java.lang.Integer for value "1"  
> > > > > java.lang.Long for value "999999999999999999"
> > > > > 
> > > > > Regards,  
> > > > > Deepak
> > > > > 
> > > > > On Tuesday, 25 September 2012 12:49:55 UTC+4, Derry O' Sullivan wrote:
> > > > > 
> > > > > > Hi Deepak,
> > > > > > 
> > > > > > I got the reverse problem (long to int) as i had loaded some content  
> > > > > > with long like fields (ES guessed the type as long) and compared to the int  
> > > > > > field values in another index. When i forced the data type to be int (by  
> > > > > > deleting/reindexing the content), everything seemed to be fine.
> > > > > > 
> > > > > > Are you getting this error when doing a get or a search? Also, how  
> > > > > > are you setting the value of 1 vs 999999999999999999? Is it done  
> > > > > > programatically via the Java API or some other way? If retrieving via the  
> > > > > > api, it would be interesting to see what .getSource().get("  
> > > > > > store\_id").getClass().getName()?
> > > > > > 
> > > > > > Derry
> > > > > > 
> > > > > > On Tuesday, 25 September 2012 09:31:18 UTC+1, Deepak Chezhian wrote:
> > > > > > 
> > > > > > > Hi Derry,
> > > > > > > 
> > > > > > > The field type seems to be fine. PFB for the mapping.
> > > > > > > 
> > > > > > > "store\_id" : { "type" : "long", "ignore\_malformed" : false }
> > > > > > > 
> > > > > > > I am getting the following exception when i tried to access that field with value "1" using Java API term query
> > > > > > > 
> > > > > > > java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
> > > > > > > 
> > > > > > > The long datatype is returned as excepted after updating that field in the same document with value "999999999999999999". Based on this, i suspect ES is returning the datatype(atlease incase of int/long) based on the value in the field.
> > > > > > > 
> > > > > > > Anyway of avoiding this?
> > > > > > > 
> > > > > > > Regards,
> > > > > > > 
> > > > > > > Deepak
> > > > > > > 
> > > > > > > On Tuesday, 25 September 2012 11:18:51 UTC+4, Derry O' Sullivan wrote:
> > > > > > > 
> > > > > > > HI Deepak,
> > > > > > > 
> > > > > > > > I had a similar problem to this - what is the mapping according to  
> > > > > > > > the index?  
> > > > > > > > ://\_mapping
> > > > > > > > 
> > > > > > > > Check for the field you want and see what the mapping type is
> > > > > > > > 
> > > > > > > > I updated the mapping for my index to use a certain data type only  
> > > > > > > > and then it seemed to return ok.  
> > > > > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/core-ty)  
> > > > > > > > pes.html
> > > > > > > > 
> > > > > > > > Also, how are you checking that it returns an int vs long - class  
> > > > > > > > name checking on the returned object?
> > > > > > > > 
> > > > > > > > Note, i'm a pretty new ES user!
> > > > > > > > 
> > > > > > > > Derry
> > > > > > > > 
> > > > > > > > On Tuesday, 25 September 2012 07:24:03 UTC+1, Deepak Chezhian wrote:
> > > > > > > > 
> > > > > > > > > Hi,
> > > > > > > > > 
> > > > > > > > > I am querying the document which contains long field. If the long  
> > > > > > > > > field contains lesser values like 1, the java api returns integer object  
> > > > > > > > > but if the values are high like 999999999999999999, the java api returns  
> > > > > > > > > long object.
> > > > > > > > > 
> > > > > > > > > Is there anyway to consistently get the long object regardless of  
> > > > > > > > > the value of the field?
> > > > > > > > > 
> > > > > > > > > Regards,  
> > > > > > > > > Deepak
> > > > > > > > 
> > > > > > > > --
> > > > 
> > > > --

--  
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/9fd690f6-d3ae-4ba4-b973-a3dd2dc47565%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9fd690f6-d3ae-4ba4-b973-a3dd2dc47565%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

_[View the full topic](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127)._
