Fetching long value from date field using Java API

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Fetching long value from date field using Java API

Adrian-2
Hi,

is it possible to fetch the stored long value of the date field using the Java
API, when this field is included in a SearchHit?

Thanks, Adrian

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Fetching long value from date field using Java API

Adrian-2
On Wed, Sep 11, 2013 at 02:58:52PM +0000, Adrian wrote:

Hi,

> is it possible to fetch the stored long value of the date field using the
> Java API, when this field is included in a SearchHit?

When trying to cast the value explicitly to Long using Elasticsearch 0.90.3, this
fails with a ClassCastException (String cannot be casted to Long), regardless
if SearchFieldHit#getValue() or SearchFieldHit#getValue() is used.

So I tried to iterate over the values using (SearchFieldHit#getValues()), but
only String objects were returned.

Does anyone have a point on how to fetch the orginal Long value? A possible
solution would be to parse the returned String - I like to avoid that if possible.

Thanks, Adrian

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Fetching long value from date field using Java API

Martijn v Groningen
Hi,

During indexing how are you defining the value for this date field? A date formatted string or as a long value?
Dates are formatted into the string format as was provided during indexing. 

Also is the date field configured to be stored separately? (stored option in mapping) 

Martijn


On 12 September 2013 10:15, Adrian <[hidden email]> wrote:
On Wed, Sep 11, 2013 at 02:58:52PM +0000, Adrian wrote:

Hi,

> is it possible to fetch the stored long value of the date field using the
> Java API, when this field is included in a SearchHit?

When trying to cast the value explicitly to Long using Elasticsearch 0.90.3, this
fails with a ClassCastException (String cannot be casted to Long), regardless
if SearchFieldHit#getValue() or SearchFieldHit#getValue() is used.

So I tried to iterate over the values using (SearchFieldHit#getValues()), but
only String objects were returned.

Does anyone have a point on how to fetch the orginal Long value? A possible
solution would be to parse the returned String - I like to avoid that if possible.

Thanks, Adrian

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.



--
Met vriendelijke groet,

Martijn van Groningen

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Fetching long value from date field using Java API

Adrian-2
On Thu, Sep 12, 2013 at 12:02:39PM +0200, Martijn v Groningen wrote:

Martijn,

> During indexing how are you defining the value for this date field? A date
> formatted string or as a long value?

I've got a formatter (using the "format" option) for this, since the documents
contain the timestamp in a formatted fashion.

> Dates are formatted into the string format as was provided during indexing.

Even if the type "date" is defined on that field? I assumed that, this
formatter was ony used for parsing the formatted date into the (internal) Long
representation.

> Also is the date field configured to be stored separately? (stored option
> in mapping)

I've set no specific store option in my mapping, so the defaults should apply.

Best regards, Adrian

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Fetching long value from date field using Java API

Martijn v Groningen
The date is just returned in the format you have provided it with. Internally the date is converted into a long, but that is only put into the inverted index, not the source you're fetching the field from. 

Do you only need to represent a date as a long? If that is the case you can represent the data as long during indexing. Just make sure the field is configured as a date field in the mapping. You can then just retrieve a long value with converting it. 



On 12 September 2013 12:55, Adrian <[hidden email]> wrote:
On Thu, Sep 12, 2013 at 12:02:39PM +0200, Martijn v Groningen wrote:

Martijn,

> During indexing how are you defining the value for this date field? A date
> formatted string or as a long value?

I've got a formatter (using the "format" option) for this, since the documents
contain the timestamp in a formatted fashion.

> Dates are formatted into the string format as was provided during indexing.

Even if the type "date" is defined on that field? I assumed that, this
formatter was ony used for parsing the formatted date into the (internal) Long
representation.

> Also is the date field configured to be stored separately? (stored option
> in mapping)

I've set no specific store option in my mapping, so the defaults should apply.

Best regards, Adrian

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.



--
Met vriendelijke groet,

Martijn van Groningen

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Fetching long value from date field using Java API

Adrian-2
On Thu, Sep 12, 2013 at 07:02:44PM +0200, Martijn v Groningen wrote:

Martijn,

> Internally the date is converted into a long, but that is only put into the
> inverted index, not the source you're fetching the field from.

I assume the internal representation is used for calculating date histogram
facets too?

> Do you only need to represent a date as a long? If that is the case you can
> represent the data as long during indexing. Just make sure the field is
> configured as a date field in the mapping. You can then just retrieve a
> long value with converting it.

The reason why I would like to fetch the long value is to provide localized
formatting (US, ISO, etc.) for date fields. Using long as basis is quite simple
then ..

Thanks for your hints,

Adrian

--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.