# Sorting on a date field

**URL:** <https://discuss.elastic.co/t/sorting-on-a-date-field/3542>\
**Category:** Elasticsearch\
**Created:** [November 10, 2010, 12:41am UTC](https://discuss.elastic.co/t/sorting-on-a-date-field/3542 "2010-11-10T00:41:47Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [November 10, 2010, 11:03pm UTC](https://discuss.elastic.co/t/sorting-on-a-date-field/3542/6 "2010-11-10T23:03:30Z")

</div>

You have two options, first is to have a numeric (long) type for the field,  
second is to have a date type, which expects (by default) an ISO formatted  
date string.

If you use the Java API, you have two options:

1. If you want the field to be numeric, then just add the "milliseconds  
since epoch" value (Date.getTime()) to your Map of values.
2. If you want the field to be a date type, then either provide the  
formatted string yourself, or, pass a Date instance as the value of the  
field, it will automatically be formatted to its ISO format and indexed.

In both options, you don't need to explicitly set the mappings. It will be  
auto detected. I say, since you already have a Date object, to index it  
(which will, in turn, be formatted as ISO formatted string, and detected as  
such in the date type).

Once you have index it, you will be able to sort it by just adding it as  
sort field in the search API.

Regarding the resolution, I am not sure I understand what you mean by  
missing resolution, but make sure the data you index does actually offer the  
resolution you expected (the Date instance you get, does it have that  
resolution?).

-shay.banon

On Thu, Nov 11, 2010 at 12:27 AM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com)wrote:

> I'm afraid I must not be understanding your advice. I guess I need more  
> specifics about how you want me to (A) map, (B) index and (C) search the  
> doc.
> 
> I read your response and then....
> 
> I tried mapping the field this way:  
> "receivedDate" : {  
> "index\_name": "receivedDate",  
> "type": "date",  
> "index": "analyzed",  
> "store": "yes",  
> "term\_vector": "no",  
> "boost": 1.0,  
> "omit\_norms": "false",  
> "omit\_term\_freq\_and\_positions": "false"  
> }
> 
> I then tried these combinations (data.getReceivedDateInUTC() returns a  
> java.util.Date):
> 
> 1. 
> 
> Map\<String, Object\> document = new HashMap\<String, Object\>();  
> document.put("receivedDate", data.getReceivedDateInUTC());
> 
> When I searched on the doc with a script:  
> .script("doc['" + sortField + "'].value")  
> it worked, but lacked precision with dates close in time (under ~ 3 min).
> 
> 1.1)  
> I indexed the doc as in 1 above, and I searched on the doc with a sort:  
> .sort(sortField)  
> I got SearchPhaseExecutionException
> 
> 1. 
> 
> Map\<String, Object\> document = new HashMap\<String, Object\>();  
> document.put("receivedDate", data.getReceivedDateInUTC().getTime());
> 
> I could not index the doc; I got:  
> java.lang.IllegalArgumentException: Invalid format: "1204351200000" is  
> malformed at "0000"
> 
> Didn't expect this to work, but was taking a guess because nothing else  
> did.
> 
> 1. 
> 
> Map\<String, Object\> document = new HashMap\<String, Object\>();  
> document.put("receivedDate",  
> Long.toString(data.getReceivedDateInUTC().getTime()));
> 
> I could not index the doc; I got:  
> java.lang.IllegalArgumentException: Invalid format: "1204351200000" is  
> malformed at "0000"  
> (same as #2 above)
> 
> Your response mentioned a date string: "The resolution of the long value is  
> based on the date string passed." Hence this test.
> 
> 1. 
> 
> Map\<String, Object\> document = new HashMap\<String, Object\>();  
> document.put("receivedDate", data.getReceivedDateInUTC().toString());
> 
> I could not index the doc; I got:  
> org.elasticsearch.index.mapper.MapperParsingException: Failed to parse  
> [receivedDate]
> 
> As in 3 above, this was a test based on my understanding of a date string  
> from your response.
> 
> ## Thanks for your help.
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Sorting-on-a-date-field-tp1873287p1879190.html](http://elasticsearch-users.115913.n3.nabble.com/Sorting-on-a-date-field-tp1873287p1879190.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

_[View the full topic](https://discuss.elastic.co/t/sorting-on-a-date-field/3542)._
