# Date type in Java API

**URL:** <https://discuss.elastic.co/t/date-type-in-java-api/7477>\
**Category:** Elasticsearch\
**Created:** [April 26, 2012, 2:27pm UTC](https://discuss.elastic.co/t/date-type-in-java-api/7477 "2012-04-26T14:27:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thinusp](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@thinusp](https://discuss.elastic.co/u/thinusp)\
**Post date:** [April 26, 2012, 2:27pm UTC](https://discuss.elastic.co/t/date-type-in-java-api/7477/1 "2012-04-26T14:27:13Z")

</div>

Hey community...

I've been struggling with the Date type in ElasticSearch. Let me first  
explain what I need, then I'll explain to you how I'm trying to get there...

I have a field in ES, configured as (copied from the output of the  
head-plugin):

myDate: {

- index: analyzed
- store: yes
- format: dateOptionalTime
- type: date

}

What I'd like to be able to do is filter (RangeFilter on SearchRequest)  
documents based on a specified Date-range. The dates I have is in the  
standard Java Date format (I'm using the Java API).

The line I use to populate this field is as follows:

...jsonBuilder().startObject().field("myDate", dateObject).endObject()...

Here, dateObject is of the standard Java Date type. To take from this is  
that I'm hoping that the API knows what to do with the Date-type object.  
Alternatively, I can place a String-formatted date (but which format?) or  
a Long value of the instant since epoch. I know not what is required  
here...

When I query the database, I then try to extract the date value with:

Date dateObject = new Date((Long) fieldMap.get("myDate").getValue()); //  
effectively GetField.getValue()

I noticed that the type in the map (fieldMap is a Map\<String, GetField\>) is  
Long, hence the type-casting.

With this implementation the filter on the search-query does not work. I  
can get the date from the returned result, but I can't get the query to  
work. I suspect the format of the query is not correct. Here is how I set  
that up:

RangeFilterBuilder dateRangeFilter = FilterBuilders.rangeFilter("myDate");  
dateRangeFilter.from(dateObject.getTime());  
dateRangeFilter.to(dateObject.getTime());

As you may or may not know, the getTime() method on a Java Date object  
returnes the time since epoch in millies. I have also tried this with the  
numeric range filter.

My question then, in three parts:

1. What is the type to use when saving the value through the API  
(setting the field)?
2. What is the variable type I can expect the get from the GetField?
3. What is the type I need to use when setting the ranges in the  
RangeFilter? (and should I use RangeFilter, or NumericRangeFilter)

Thank you, once again.

Kind regards,  
Thinus

---

<div class="post-metadata">

**Author:** ![jschelle\_2](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jschelle\_2](https://discuss.elastic.co/u/jschelle_2)\
**Post date:** [April 26, 2012, 2:35pm UTC](https://discuss.elastic.co/t/date-type-in-java-api/7477/2 "2012-04-26T14:35:41Z")

</div>

This may help:

> **[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.

On Thursday, April 26, 2012 10:27:13 AM UTC-4, Thinus Prinsloo wrote:

> Hey community...
> 
> I've been struggling with the Date type in Elasticsearch. Let me first  
> explain what I need, then I'll explain to you how I'm trying to get there...
> 
> I have a field in ES, configured as (copied from the output of the  
> head-plugin):
> 
> myDate: {
> 
> - index: analyzed
> - store: yes
> - format: dateOptionalTime
> - type: date
> 
> }
> 
> What I'd like to be able to do is filter (RangeFilter on SearchRequest)  
> documents based on a specified Date-range. The dates I have is in the  
> standard Java Date format (I'm using the Java API).
> 
> The line I use to populate this field is as follows:
> 
> ...jsonBuilder().startObject().field("myDate", dateObject).endObject()...
> 
> Here, dateObject is of the standard Java Date type. To take from this is  
> that I'm hoping that the API knows what to do with the Date-type object.  
> Alternatively, I can place a String-formatted date (but which format?) or  
> a Long value of the instant since epoch. I know not what is required  
> here...
> 
> When I query the database, I then try to extract the date value with:
> 
> Date dateObject = new Date((Long) fieldMap.get("myDate").getValue()); //  
> effectively GetField.getValue()
> 
> I noticed that the type in the map (fieldMap is a Map\<String, GetField\>)  
> is Long, hence the type-casting.
> 
> With this implementation the filter on the search-query does not work. I  
> can get the date from the returned result, but I can't get the query to  
> work. I suspect the format of the query is not correct. Here is how I set  
> that up:
> 
> RangeFilterBuilder dateRangeFilter = FilterBuilders.rangeFilter("myDate");  
> dateRangeFilter.from(dateObject.getTime());  
> dateRangeFilter.to(dateObject.getTime());
> 
> As you may or may not know, the getTime() method on a Java Date object  
> returnes the time since epoch in millies. I have also tried this with the  
> numeric range filter.
> 
> My question then, in three parts:
> 
> 1. What is the type to use when saving the value through the API  
> (setting the field)?
> 2. What is the variable type I can expect the get from the GetField?
> 3. What is the type I need to use when setting the ranges in the  
> RangeFilter? (and should I use RangeFilter, or NumericRangeFilter)
> 
> Thank you, once again.
> 
> Kind regards,  
> Thinus

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 27, 2012, 11:21am UTC](https://discuss.elastic.co/t/date-type-in-java-api/7477/3 "2012-04-27T11:21:31Z")

</div>

Internally, a date type is stored as a long value milliseconds since the  
epoch. When you provide a Date object to teh range filter, it will be  
formatted as the default ISO format (dateOptionalTime). You will get the  
value as the string formatted value though (String). I do wonder though why  
do you store the field, cause if you don't store it, it will be  
automatically extracted from \_source.

In general, what you do is fine, can you maybe share a sample code that  
repro this? (create index, put mapping, index a sample doc, and show the  
search failure)?

On Thu, Apr 26, 2012 at 5:27 PM, Thinus Prinsloo  
[thinus.prinsloo@gmail.com](mailto:thinus.prinsloo@gmail.com)wrote:

> Hey community...
> 
> I've been struggling with the Date type in Elasticsearch. Let me first  
> explain what I need, then I'll explain to you how I'm trying to get there...
> 
> I have a field in ES, configured as (copied from the output of the  
> head-plugin):
> 
> myDate: {
> 
> - index: analyzed
> - store: yes
> - format: dateOptionalTime
> - type: date
> 
> }
> 
> What I'd like to be able to do is filter (RangeFilter on SearchRequest)  
> documents based on a specified Date-range. The dates I have is in the  
> standard Java Date format (I'm using the Java API).
> 
> The line I use to populate this field is as follows:
> 
> ...jsonBuilder().startObject().field("myDate", dateObject).endObject()...
> 
> Here, dateObject is of the standard Java Date type. To take from this is  
> that I'm hoping that the API knows what to do with the Date-type object.  
> Alternatively, I can place a String-formatted date (but which format?) or  
> a Long value of the instant since epoch. I know not what is required  
> here...
> 
> When I query the database, I then try to extract the date value with:
> 
> Date dateObject = new Date((Long) fieldMap.get("myDate").getValue()); //  
> effectively GetField.getValue()
> 
> I noticed that the type in the map (fieldMap is a Map\<String, GetField\>)  
> is Long, hence the type-casting.
> 
> With this implementation the filter on the search-query does not work. I  
> can get the date from the returned result, but I can't get the query to  
> work. I suspect the format of the query is not correct. Here is how I set  
> that up:
> 
> RangeFilterBuilder dateRangeFilter = FilterBuilders.rangeFilter("myDate");  
> dateRangeFilter.from(dateObject.getTime());  
> dateRangeFilter.to(dateObject.getTime());
> 
> As you may or may not know, the getTime() method on a Java Date object  
> returnes the time since epoch in millies. I have also tried this with the  
> numeric range filter.
> 
> My question then, in three parts:
> 
> 1. What is the type to use when saving the value through the API  
> (setting the field)?
> 2. What is the variable type I can expect the get from the GetField?
> 3. What is the type I need to use when setting the ranges in the  
> RangeFilter? (and should I use RangeFilter, or NumericRangeFilter)
> 
> Thank you, once again.
> 
> Kind regards,  
> Thinus

---

<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, 3:30am UTC](https://discuss.elastic.co/t/date-type-in-java-api/7477/4 "2017-07-06T03:30:52Z")

</div>


