Sort is giving invalid results

Hi,

I am using transport client in my application. When i search against multiple indices (user-*) and sort them by date it is giving invalid result set. Can some one help me to fix that.

Thanks
Pranesh

May be you can share an example?

Hi David,

This is my search query and result

POST /user-x187/_search?from=0&size=10
{
    "_source" : ["policyNumer",
               "userId", "companyId", "dateOfJoining", "phoneNumber"], 
    "sort": [
       {
          "dateOfJoining": {
              "order": "desc"
          }
       }
    ], 
     "query": {
        "multi_match": {
            "query": "234567891", 
            "fields": [
               "policyNumer.raw",
               "policyNumer",
               "userId",
               "companyId",
               "phoneNumber"
            ],
            "minimum_should_match":"100"
        }
    },
    "highlight" : {
        "require_field_match": true,
        "fields" : {
            "userId" : {},
            "policyNumer":{},
            "policyNumer.raw":{},
            "companyId":{},
            "phoneNumber":{}
        }
    }
}

Result:

{  
   "searchResult":{  
      "took":273,
      "timed_out":false,
      "_shards":{  
         "total":1920,
         "successful":1920,
         "failed":0
      },
      "hits":{  
         "total":19,
         "max_score":null,
         "hits":[  
            {  
               "_index":"user-x678",
               "_type":"user",
               "_id":"SEARCH00011",
               "_score":null,
               "_source":{  
                  "dateOfJoining":"12/25/2010, 12:00:00 AM -0500"
                  "policyNumber":"SPRINT101NUMBER6",
                  "phoneNumber":"234567891"
                  "userId":"SEARCH00011",
                  "companyId":"x678"
               },
               "highlight":{  
                  "phoneNumber":[  
                     "<em>234567891</em>"
                  ]
               },
               "sort":[  
                  "12/25/2010, 12:00:00 AM -0500"
               ]
            },
            {  
               "_index":"user-x678",
               "_type":"user",
               "_id":"SEARCH00009",
               "_score":null,
               "_source":{  
                  "dateOfJoining":"08/09/2015, 12:00:00 AM -0400"
                  "policyNumber":"SPRINT101NUMBER9",
                  "phoneNumber":"234567891"
                  "userId":"SEARCH00009",
                  "companyId":"x678"
               },
               "highlight":{  
                  "phoneNumber":[  
                     "<em>234567891</em>"
                  ]
               },
               "sort":[  
                  "08/09/2015, 12:00:00 AM -0400"
               ]
            }
        ]
    }
}

Please format your code with </> icon. I updated your post.

12/25/2010 > 08/09/2015 when you compare Strings. And I'm pretty sure that your mapping will tell you that your field dateOfJoining is actually a String, right?

So if you want to sort on a date, you need to make sure that this field is a date.
It means, create the right mapping for your documents.

I hope this helps.

Thanks David. yes you are right my mapping was wrong.

"dateOfJoining":{"type":"string","index":"not_analyzed","doc_values":true}, I will update the mapping.

Thanks,
Pranesh