# Sorting on date field issue

**URL:** <https://discuss.elastic.co/t/sorting-on-date-field-issue/3949>\
**Category:** Elasticsearch\
**Created:** [February 18, 2011, 7:17pm UTC](https://discuss.elastic.co/t/sorting-on-date-field-issue/3949 "2011-02-18T19:17:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)\
**Post date:** [February 18, 2011, 7:17pm UTC](https://discuss.elastic.co/t/sorting-on-date-field-issue/3949/1 "2011-02-18T19:17:59Z")

</div>

I'm having an issue with sorting on a date field. The field is defined  
in my mapping as:

```
        "created_on" : {"type" : "date", "format" : "YYYY-MM-DD

```

HH:mm:ss"},

When I do a simple matchAll + sort query, the first result is way  
newer than I'd expect:

$ curl '[http://localhost:9200/places/\_search?pretty=true](http://localhost:9200/places/_search?pretty=true)' -d'{"sort":  
["created\_on"], "query": {"match\_all": {}}, "fields": ["created\_on"],  
"size": 1}'

{  
"took" : 1,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 1788,  
"max\_score" : null,  
"hits" : [ {  
"\_index" : "places",  
"\_type" : "place",  
"\_id" : "436",  
"\_score" : null,  
"fields" : {  
"created\_on" : "2010-12-01 00:33:34"  
},  
"sort" : [1262306014000]  
} ]  
}  
}

But there're clearly docs that are older. For example, the one with  
\_id 435:

$ curl '[http://localhost:9200/places/place/435](http://localhost:9200/places/place/435)'

{"\_index":"places","\_type":"place","\_id":"435", "\_source" : {....,  
"created\_on": "2010-11-30 23:01:25", ......}}

Am I doing something wrong?

---

<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:** [February 18, 2011, 8:16pm UTC](https://discuss.elastic.co/t/sorting-on-date-field-issue/3949/2 "2011-02-18T20:16:07Z")

</div>

Gist a curl recreation.  
On Friday, February 18, 2011 at 9:17 PM, Andrei wrote:

> I'm having an issue with sorting on a date field. The field is defined  
> in my mapping as:
> 
> "created\_on" : {"type" : "date", "format" : "YYYY-MM-DD  
> HH:mm:ss"},
> 
> When I do a simple matchAll + sort query, the first result is way  
> newer than I'd expect:
> 
> $ curl '[http://localhost:9200/places/\_search?pretty=true](http://localhost:9200/places/_search?pretty=true)' -d'{"sort":  
> ["created\_on"], "query": {"match\_all": {}}, "fields": ["created\_on"],  
> "size": 1}'
> 
> {  
> "took" : 1,  
> "\_shards" : {  
> "total" : 1,  
> "successful" : 1,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 1788,  
> "max\_score" : null,  
> "hits" : [ {  
> "\_index" : "places",  
> "\_type" : "place",  
> "\_id" : "436",  
> "\_score" : null,  
> "fields" : {  
> "created\_on" : "2010-12-01 00:33:34"  
> },  
> "sort" : [1262306014000]  
> } ]  
> }  
> }
> 
> But there're clearly docs that are older. For example, the one with  
> \_id 435:
> 
> $ curl '[http://localhost:9200/places/place/435](http://localhost:9200/places/place/435)'
> 
> {"\_index":"places","\_type":"place","\_id":"435", "\_source" : {....,  
> "created\_on": "2010-11-30 23:01:25", ......}}
> 
> Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei/32/2856_2.png) [@Andrei](https://discuss.elastic.co/u/Andrei)\
**Post date:** [February 19, 2011, 12:12am UTC](https://discuss.elastic.co/t/sorting-on-date-field-issue/3949/3 "2011-02-19T00:12:27Z")

</div>

Never mind, I had a wrong date format in the field mapping - used DD  
instead of dd for the day of the month. 🙂

On Feb 18, 12:16 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Gist a curl recreation.
> 
> On Friday, February 18, 2011 at 9:17 PM, Andrei wrote:
> 
> > I'm having an issue with sorting on a date field. The field is defined  
> > in my mapping as:
> 
> > "created\_on" : {"type" : "date", "format" : "YYYY-MM-DD  
> > HH:mm:ss"},
> 
> > When I do a simple matchAll + sort query, the first result is way  
> > newer than I'd expect:
> 
> > $ curl '[http://localhost:9200/places/\_search?pretty=true'-d'](http://localhost:9200/places/_search?pretty=true'-d'){"sort":  
> > ["created\_on"], "query": {"match\_all": {}}, "fields": ["created\_on"],  
> > "size": 1}'
> 
> > {  
> > "took" : 1,  
> > "\_shards" : {  
> > "total" : 1,  
> > "successful" : 1,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 1788,  
> > "max\_score" : null,  
> > "hits" : [ {  
> > "\_index" : "places",  
> > "\_type" : "place",  
> > "\_id" : "436",  
> > "\_score" : null,  
> > "fields" : {  
> > "created\_on" : "2010-12-01 00:33:34"  
> > },  
> > "sort" : [1262306014000]  
> > } ]  
> > }  
> > }
> 
> > But there're clearly docs that are older. For example, the one with  
> > \_id 435:
> 
> > $ curl '[http://localhost:9200/places/place/435](http://localhost:9200/places/place/435)'
> 
> > {"\_index":"places","\_type":"place","\_id":"435", "\_source" : {....,  
> > "created\_on": "2010-11-30 23:01:25", ......}}
> 
> > Am I doing something wrong?

---

<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, 4:11am UTC](https://discuss.elastic.co/t/sorting-on-date-field-issue/3949/4 "2017-07-06T04:11:49Z")

</div>


