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

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?

--

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?

--

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?

--

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?

--

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?

--

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:

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.comwrote:

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?

--

--

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/) 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:

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<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?

--

--

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/ff2f3a76-f4be-4669-8fdc-c20b2efb1c6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.