# Uncertain field types when extracting fields from getSource() (java api)

**URL:** <https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432>\
**Category:** Elasticsearch\
**Created:** [January 20, 2013, 7:43pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432 "2013-01-20T19:43:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Burak\_Emre\_Kabakci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/burak_emre_kabakci/32/2212_2.png) [@Burak\_Emre\_Kabakci](https://discuss.elastic.co/u/Burak_Emre_Kabakci)\
**Post date:** [January 20, 2013, 7:43pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/1 "2013-01-20T19:43:43Z")

</div>

I'm trying to extract fields from  
org.elasticsearch.search.SearchHit.getSource() but I'm having difficulties  
of determining types of fields. For example I have a field named rank defined  
as long with \_mapping and when I extract it from Map\<String, Object\> I  
couldn't be able to cast it Long because it returns as java.lang.Double or  
java.lang.Integer. I encountered same problem for short type and it is  
returned as java.lang.Integer Is it a bug or am I missing something?

--

---

<div class="post-metadata">

**Author:** ![Mohammady\_Mahdy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammady_mahdy/32/2128_2.png) [@Mohammady\_Mahdy](https://discuss.elastic.co/u/Mohammady_Mahdy)\
**Post date:** [January 21, 2013, 2:03pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/2 "2013-01-21T14:03:16Z")

</div>

I've seen this happen a couple of times.. specially when parsing geo coords.

On Sunday, January 20, 2013 11:43:43 PM UTC+4, Burak Emre Kabakcı wrote:

> I'm trying to extract fields from  
> org.elasticsearch.search.SearchHit.getSource() but I'm having  
> difficulties of determining types of fields. For example I have a field  
> named rank defined as long with \_mapping and when I extract it from Map\<String,  
> Object\> I couldn't be able to cast it Long because it returns as  
> java.lang.Double orjava.lang.Integer. I encountered same problem for  
> short type and it is returned as java.lang.Integer Is it a bug or am I  
> missing something?

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 22, 2013, 6:35pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/3 "2013-01-22T18:35:45Z")

</div>

When you are calling getSource(), you are getting the source that you  
supplied during indexing. So, when you are trying to extract a field from  
it, you are extracting this field from JSON object and Elasticsearch  
mapping doesn't play any role in this process.

On Monday, January 21, 2013 9:03:16 AM UTC-5, Mo wrote:

> I've seen this happen a couple of times.. specially when parsing geo  
> coords.
> 
> On Sunday, January 20, 2013 11:43:43 PM UTC+4, Burak Emre Kabakcı wrote:
> 
> > I'm trying to extract fields from  
> > org.elasticsearch.search.SearchHit.getSource() but I'm having  
> > difficulties of determining types of fields. For example I have a field  
> > named rank defined as long with \_mapping and when I extract it from Map\<String,  
> > Object\> I couldn't be able to cast it Long because it returns as  
> > java.lang.Double orjava.lang.Integer. I encountered same problem for  
> > short type and it is returned as java.lang.Integer Is it a bug or am I  
> > missing something?

--

---

<div class="post-metadata">

**Author:** ![Burak\_Emre\_Kabakci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/burak_emre_kabakci/32/2212_2.png) [@Burak\_Emre\_Kabakci](https://discuss.elastic.co/u/Burak_Emre_Kabakci)\
**Post date:** [January 22, 2013, 7:13pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/4 "2013-01-22T19:13:49Z")

</div>

Thanks for your reply. As far as I understand Elasticsearch uses Jackson  
library. As you know getSource() returns Map\<String, Object\> and we don't  
have any idea about real types of those Objects. We need to cast it to the  
type we want to use with try / catch exceptions, it's not efficient and  
proper way to do it. It would be great if it  
returns org.codehaus.jackson.JsonNode. It provides asDouble(), asFloat()  
etc. so it would be easy to extract fields from it.

On Tuesday, January 22, 2013 8:35:45 PM UTC+2, Igor Motov wrote:

> When you are calling getSource(), you are getting the source that you  
> supplied during indexing. So, when you are trying to extract a field from  
> it, you are extracting this field from JSON object and Elasticsearch  
> mapping doesn't play any role in this process.
> 
> On Monday, January 21, 2013 9:03:16 AM UTC-5, Mo wrote:
> 
> > I've seen this happen a couple of times.. specially when parsing geo  
> > coords.
> > 
> > On Sunday, January 20, 2013 11:43:43 PM UTC+4, Burak Emre Kabakcı wrote:
> > 
> > > I'm trying to extract fields from  
> > > org.elasticsearch.search.SearchHit.getSource() but I'm having  
> > > difficulties of determining types of fields. For example I have a field  
> > > named rank defined as long with \_mapping and when I extract it from Map\<String,  
> > > Object\> I couldn't be able to cast it Long because it returns as  
> > > java.lang.Double orjava.lang.Integer. I encountered same problem for  
> > > short type and it is returned as java.lang.Integer Is it a bug or am I  
> > > missing something?

--

---

<div class="post-metadata">

**Author:** ![Burak\_Emre\_Kabakci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/burak_emre_kabakci/32/2212_2.png) [@Burak\_Emre\_Kabakci](https://discuss.elastic.co/u/Burak_Emre_Kabakci)\
**Post date:** [January 22, 2013, 7:25pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/5 "2013-01-22T19:25:23Z")

</div>

Thanks for your reply. As far as I understand Elasticsearch uses Jackson  
library. As you know getSource() returns Map\<String, Object\> and we don't  
have any idea about real types of those Objects. We need to cast it to the  
type we want to use with try / catch exceptions, it's not efficient and  
proper way to do it. It would be great if it returns  
org.codehaus.jackson.JsonNode. We could easily create classes via  
data-binding or extract fields using getDouble(), getFloat() etc.  
with JsonNode so it would be really easy to handle json source.

On Tuesday, January 22, 2013 8:35:45 PM UTC+2, Igor Motov wrote:

> When you are calling getSource(), you are getting the source that you  
> supplied during indexing. So, when you are trying to extract a field from  
> it, you are extracting this field from JSON object and Elasticsearch  
> mapping doesn't play any role in this process.
> 
> On Monday, January 21, 2013 9:03:16 AM UTC-5, Mo wrote:
> 
> > I've seen this happen a couple of times.. specially when parsing geo  
> > coords.
> > 
> > On Sunday, January 20, 2013 11:43:43 PM UTC+4, Burak Emre Kabakcı wrote:
> > 
> > > I'm trying to extract fields from  
> > > org.elasticsearch.search.SearchHit.getSource() but I'm having  
> > > difficulties of determining types of fields. For example I have a field  
> > > named rank defined as long with \_mapping and when I extract it from Map\<String,  
> > > Object\> I couldn't be able to cast it Long because it returns as  
> > > java.lang.Double orjava.lang.Integer. I encountered same problem for  
> > > short type and it is returned as java.lang.Integer Is it a bug or am I  
> > > missing something?

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 22, 2013, 7:30pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/6 "2013-01-22T19:30:11Z")

</div>

You would need to keep track of the mapping yourself. As you noticed,  
Elasticsearch returns Objects in the API. Someone attempted to build a  
mapping framework around Elasticsearch, but it appears to have been  
abandoned:

> **[GitHub - aloiscochard/elasticsearch-osem: ElasticSearch OSEM](https://github.com/aloiscochard/elasticsearch-osem)**
>
> ElasticSearch OSEM. Contribute to aloiscochard/elasticsearch-osem development by creating an account on GitHub.

You can follow the same approach, try looking at that code. I bypassed  
dynamic mapping and built an extraction library that deals with a ton of  
ugly instanceof checks and cast the object to the appropriate type.

Cheers,

Ivan

On Tue, Jan 22, 2013 at 11:13 AM, Burak Emre Kabakcı  
[emrekabakci@gmail.com](mailto:emrekabakci@gmail.com)wrote:

> Thanks for your reply. As far as I understand Elasticsearch uses Jackson  
> library. As you know getSource() returns Map\<String, Object\> and we don't  
> have any idea about real types of those Objects. We need to cast it to the  
> type we want to use with try / catch exceptions, it's not efficient and  
> proper way to do it. It would be great if it  
> returns org.codehaus.jackson.JsonNode. It provides asDouble(), asFloat()  
> etc. so it would be easy to extract fields from it.
> 
> On Tuesday, January 22, 2013 8:35:45 PM UTC+2, Igor Motov wrote:
> 
> > When you are calling getSource(), you are getting the source that you  
> > supplied during indexing. So, when you are trying to extract a field from  
> > it, you are extracting this field from JSON object and Elasticsearch  
> > mapping doesn't play any role in this process.
> > 
> > On Monday, January 21, 2013 9:03:16 AM UTC-5, Mo wrote:
> > 
> > > I've seen this happen a couple of times.. specially when parsing geo  
> > > coords.
> > > 
> > > On Sunday, January 20, 2013 11:43:43 PM UTC+4, Burak Emre Kabakcı wrote:
> > > 
> > > > I'm trying to extract fields from org.elasticsearch.search.\*\*  
> > > > SearchHit.getSource() but I'm having difficulties of determining types  
> > > > of fields. For example I have a field named rank defined as long with  
> > > > \_mapping and when I extract it from Map\<String, Object\> I couldn't be  
> > > > able to cast it Long because it returns as java.lang.Double orjava.\*\*  
> > > > lang.Integer. I encountered same problem for short type and it is  
> > > > returned as java.lang.Integer Is it a bug or am I missing something?
> > > 
> > > --

--

---

<div class="post-metadata">

**Author:** ![Burak\_Emre\_Kabakci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/burak_emre_kabakci/32/2212_2.png) [@Burak\_Emre\_Kabakci](https://discuss.elastic.co/u/Burak_Emre_Kabakci)\
**Post date:** [January 28, 2013, 3:43am UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/7 "2013-01-28T03:43:57Z")

</div>

Using Reflections for creating dynamic objects doesn't seem a good idea for  
me for performance penalty but for mapping objects ModelMapper  
([http://modelmapper.org/](http://modelmapper.org/)) works great for me.

On Tuesday, January 22, 2013 9:30:11 PM UTC+2, Ivan Brusic wrote:

> You would need to keep track of the mapping yourself. As you noticed,  
> Elasticsearch returns Objects in the API. Someone attempted to build a  
> mapping framework around Elasticsearch, but it appears to have been  
> abandoned:
> 
> [GitHub - aloiscochard/elasticsearch-osem: ElasticSearch OSEM](https://github.com/aloiscochard/elasticsearch-osem)
> 
> You can follow the same approach, try looking at that code. I bypassed  
> dynamic mapping and built an extraction library that deals with a ton of  
> ugly instanceof checks and cast the object to the appropriate type.
> 
> Cheers,
> 
> Ivan
> 
> On Tue, Jan 22, 2013 at 11:13 AM, Burak Emre Kabakcı \<[emrek...@gmail.com](mailto:emrek...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Thanks for your reply. As far as I understand Elasticsearch uses Jackson  
> > library. As you know getSource() returns Map\<String, Object\> and we don't  
> > have any idea about real types of those Objects. We need to cast it to the  
> > type we want to use with try / catch exceptions, it's not efficient and  
> > proper way to do it. It would be great if it  
> > returns org.codehaus.jackson.JsonNode. It provides asDouble(), asFloat()  
> > etc. so it would be easy to extract fields from it.
> > 
> > On Tuesday, January 22, 2013 8:35:45 PM UTC+2, Igor Motov wrote:
> > 
> > > When you are calling getSource(), you are getting the source that you  
> > > supplied during indexing. So, when you are trying to extract a field from  
> > > it, you are extracting this field from JSON object and Elasticsearch  
> > > mapping doesn't play any role in this process.
> > > 
> > > On Monday, January 21, 2013 9:03:16 AM UTC-5, Mo wrote:
> > > 
> > > > I've seen this happen a couple of times.. specially when parsing geo  
> > > > coords.
> > > > 
> > > > On Sunday, January 20, 2013 11:43:43 PM UTC+4, Burak Emre Kabakcı wrote:
> > > > 
> > > > > I'm trying to extract fields from org.elasticsearch.search.\*\*  
> > > > > SearchHit.getSource() but I'm having difficulties of determining  
> > > > > types of fields. For example I have a field named rank defined as long  
> > > > > with \_mapping and when I extract it from Map\<String, Object\> I  
> > > > > couldn't be able to cast it Long because it returns as  
> > > > > java.lang.Double orjava.\*\*lang.Integer. I encountered same problem  
> > > > > for short type and it is returned as java.lang.Integer Is it a bug  
> > > > > or am I missing something?
> > > > 
> > > > --

--

---

<div class="post-metadata">

**Author:** ![Simon\_Brandhof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_brandhof/32/1101_2.png) [@Simon\_Brandhof](https://discuss.elastic.co/u/Simon_Brandhof)\
**Post date:** [November 20, 2014, 7:43pm UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/8 "2014-11-20T19:43:50Z")

</div>

A workaround is to cast the value into Number and then to call  
Number#longValue().

--  
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/ff2f3a76-f4be-4669-8fdc-c20b2efb1c6e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ff2f3a76-f4be-4669-8fdc-c20b2efb1c6e%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, 12:48am UTC](https://discuss.elastic.co/t/uncertain-field-types-when-extracting-fields-from-getsource-java-api/10432/9 "2017-07-06T00:48:35Z")

</div>


