# 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:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_Chezhian](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@Deepak\_Chezhian](https://discuss.elastic.co/u/Deepak_Chezhian)\
**Post date:** [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/1 "2012-09-25T06:24:03Z")

</div>

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

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [September 25, 2012, 7:18am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/2 "2012-09-25T07:18:51Z")

</div>

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](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

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

--

---

<div class="post-metadata">

**Author:** ![Deepak\_Chezhian](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@Deepak\_Chezhian](https://discuss.elastic.co/u/Deepak_Chezhian)\
**Post date:** [September 25, 2012, 8:31am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/3 "2012-09-25T08:31:18Z")

</div>

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-types.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

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [September 25, 2012, 8:49am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/4 "2012-09-25T08:49:55Z")

</div>

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-types.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

--

---

<div class="post-metadata">

**Author:** ![Deepak\_Chezhian](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@Deepak\_Chezhian](https://discuss.elastic.co/u/Deepak_Chezhian)\
**Post date:** [September 25, 2012, 10:26am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/5 "2012-09-25T10:26:18Z")

</div>

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.

\*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-types.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

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [September 25, 2012, 10:29am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/6 "2012-09-25T10:29:34Z")

</div>

On 25 September 2012 11:26, Deepak Chezhian [cdeepakcse@gmail.com](mailto:cdeepakcse@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?

> <https://stackoverflow.com/questions/769963/javas-l-number-long-specification>

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-)\*\*  
> > > > types.html[http://www.elasticsearch.org/guide/reference/mapping/core-types.html](http://www.elasticsearch.org/guide/reference/mapping/core-types.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
> > > > 
> > > > --

--

---

<div class="post-metadata">

**Author:** ![Deepak\_Chezhian](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@Deepak\_Chezhian](https://discuss.elastic.co/u/Deepak_Chezhian)\
**Post date:** [September 25, 2012, 10:37am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/7 "2012-09-25T10:37:09Z")

</div>

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.

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)\<javascript:\>
> 
> > 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?
> 
> [http://stackoverflow.com/questions/769963/javas-l-number-long-specification-question](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-)\*\*  
> > > > > types.html[http://www.elasticsearch.org/guide/reference/mapping/core-types.html](http://www.elasticsearch.org/guide/reference/mapping/core-types.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
> > > > > 
> > > > > --

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [September 25, 2012, 10:39am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/8 "2012-09-25T10:39:03Z")

</div>

On 25 September 2012 11:37, Deepak Chezhian [cdeepakcse@gmail.com](mailto:cdeepakcse@gmail.com) 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?  
> > [http://stackoverflow.com/\*\*questions/769963/javas-l-](http://stackoverflow.com/**questions/769963/javas-l-)\*\*  
> > number-long-specification-\*\*question[http://stackoverflow.com/questions/769963/javas-l-number-long-specification-question](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("\*  
> > > > \*s**tore\_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?  
> > > > > > ://\_ **map** ping
> > > > > > 
> > > > > > 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/ **g** uide/reference/mapping/core-**ty)\*\*  
> > > > > > pes.html[http://www.elasticsearch.org/guide/reference/mapping/core-types.html](http://www.elasticsearch.org/guide/reference/mapping/core-types.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
> > > > > > 
> > > > > > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [September 25, 2012, 10:50am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/9 "2012-09-25T10:50:56Z")

</div>

Looks like mappings are only used for ingestion:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

According to the search docs:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

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 [derryos@gmail.com](mailto:derryos@gmail.com) wrote:

> On 25 September 2012 11:37, Deepak Chezhian [cdeepakcse@gmail.com](mailto:cdeepakcse@gmail.com) 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?  
> > > [http://stackoverflow.com/\*\*questions/769963/javas-l-](http://stackoverflow.com/**questions/769963/javas-l-)\*\*  
> > > number-long-specification-\*\*question[http://stackoverflow.com/questions/769963/javas-l-number-long-specification-question](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("  
> > > > > **s** tore\_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?  
> > > > > > > ://\_ **map** ping
> > > > > > > 
> > > > > > > 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/ **g** uide/reference/mapping/core-**ty)\*\*  
> > > > > > > pes.html[http://www.elasticsearch.org/guide/reference/mapping/core-types.html](http://www.elasticsearch.org/guide/reference/mapping/core-types.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
> > > > > > > 
> > > > > > > --
> > > 
> > > --

--

---

<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).

---

<div class="post-metadata">

**Author:** ![pratap](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@pratap](https://discuss.elastic.co/u/pratap)\
**Post date:** [August 4, 2016, 2:38pm UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/11 "2016-08-04T14:38:42Z")

</div>

Hi,  
I'm also facing the same issue,  
Did one managed to get solution for this problem ?  
If yes please share solution thanks.

My mapping is

PUT test  
{  
"mappings": {  
"testType": {  
"properties": {  
"TEST\_ID": {  
"type": "long",  
"index": "not\_analyzed"  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![lukasSirhal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukassirhal1/32/13873_2.png) [@lukasSirhal1](https://discuss.elastic.co/u/lukasSirhal1)\
**Post date:** [May 25, 2017, 7:50am UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/12 "2017-05-25T07:50:48Z")

</div>

Hi, is there any new solution?  
I just find this problem in our project with es 5.1.1 version.  
The problem is same, I have long fiend in mapping and JAVA API return Integer

Thanks for reply

---

<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, 10:00pm UTC](https://discuss.elastic.co/t/long-field-is-returned-as-integer-if-the-value-is-less-in-java-api/9127/13 "2017-07-05T22:00:04Z")

</div>


