aashish
aashish

Reputation: 2603

elasticsearch sort by date is not working

I am having the problem with sorting by date on elasticsearch. As per the documentation provided by elasticsearch http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_sorting.html

I used the following code to sort by date

{"sort":{"posted_on":{"order":"desc"}}}

The following is the result

{
"took": 3,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
},
"hits": {
    "total": 305,
    "max_score": null,
    "hits": [
        {
            "_index": "ads",
            "_type": "ad",
            "_id": "52d6945f1d41c812bf000004",
            "_score": null,
            "_source": {
                "category": "Cars and Bikes",
                "description": "Innova 2010(V) DC",
                "posted_on": "2014-09-04T18:11:57+00:00",
                "slug_id": "innova-2010-v-dc",
                "title": "Innova 2010(V) DC"
            },
            "sort": [
                "57"
            ]
        }
        ,
        {
            "_index": "ads",
            "_type": "ad",
            "_id": "5408accf1d41c8b8f1000001",
            "_score": null,
            "_source": {
                "category": "",
                "description": "rrrrrrrrrrfsadsadfsadfsa",
                "posted_on": "2014-09-04T18:11:57+00:00",
                "slug_id": "test",
                "title": "test"
            },
            "sort": [
                "57"
            ]
        }
        ,
        {
            "_index": "ads",
            "_type": "ad",
            "_id": "52d6945f1d41c812bf000003",
            "_score": null,
            "_source": {
                "category": "Cars and Bikes",
                "description": "Indica DLS 2006 for immediate sale",
                "posted_on": "2015-02-16T12:46:10+00:00",
                "slug_id": "indica-dls",
                "title": "Indica DLS"
            },
            "sort": [
                "46"
            ]
        }
        ,
        {
            "_index": "ads",
            "_type": "ad",
            "_id": "52d694981d41c812d800000a",
            "_score": null,
            "_source": {
                "category": "Automotive",
                "description": "MAHINDRA XYLO 2011 MAY Hyderabad registration for immediate sale cash party ",
                "posted_on": "2015-02-16T12:46:10+00:00",
                "slug_id": "mahindra-xylo-2011-may-hyderabad-registration",
                "title": "MAHINDRA XYLO 2011 MAY Hyderabad registration"
            },
            "sort": [
                "46"
            ]
        }
        ,
        {
            "_index": "ads",
            "_type": "ad",
            "_id": "52d694981d41c812d8000010",
            "_score": null,
            "_source": {
                "category": "Automotive",
                "description": "SWIFT LDI 2008 Black,single owner, Hyderabad Registration, Excellent Condition",
                "posted_on": "2015-02-16T12:46:10+00:00",
                "slug_id": "swift-ldi-2008-black",
                "title": "SWIFT LDI 2008 Black"
            },
            "sort": [
                "46"
            ]
        }

Is their anything wrong in query. Thanks, Aashish

Upvotes: 5

Views: 10019

Answers (1)

Ricardo ACB
Ricardo ACB

Reputation: 268

Just to answer the question, It's like aashish said. When this things happens you might want to take a look on your mapping field and make sure that the type of the field it's "Date".

You may also want to specify the date format as it says in here

Upvotes: 1

Related Questions