# Fetching long value from date field using Java API

**URL:** <https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558>\
**Category:** Elasticsearch\
**Created:** [September 11, 2013, 2:58pm UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558 "2013-09-11T14:58:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [September 11, 2013, 2:58pm UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/1 "2013-09-11T14:58:52Z")

</div>

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [September 12, 2013, 8:15am UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/2 "2013-09-12T08:15:41Z")

</div>

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 12, 2013, 10:02am UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/3 "2013-09-12T10:02:39Z")

</div>

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 [google@core.kyubu.de](mailto:google@core.kyubu.de) 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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [September 12, 2013, 10:55am UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/4 "2013-09-12T10:55:34Z")

</div>

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 12, 2013, 5:02pm UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/5 "2013-09-12T17:02:44Z")

</div>

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 [google@core.kyubu.de](mailto:google@core.kyubu.de) 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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [September 12, 2013, 7:01pm UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/6 "2013-09-12T19:01:42Z")

</div>

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:16am UTC](https://discuss.elastic.co/t/fetching-long-value-from-date-field-using-java-api/13558/7 "2017-07-06T02:16:50Z")

</div>


