0
votes

When I am doing wildcard query the result I am getting have different order from inserted order. for example, suppose I have inserted 4 items in "name" field Pamela,Patricia,Pandora,Paige.

And doing wildcard query with "P*". result I am recieving is Patricia,Pamela,Paige,Pandora. Is there any way I can get the results in same order as insertion. expected output should be Pamela,Patricia,Pandora,Paige. Also ordering of results are varying machine to machine.

I am using elastic search version 2.4. I want to do this on same version, Since it is in production.

1

1 Answers

3
votes

While creating your index you have to add timestamp mapping, you can do so by using the below query

PUT my_index
{
  "mappings": {
    "my_type": {
      "_timestamp": { 
        "enabled": true
      }
    }
  }
}

After that, you can query using this timestamp as following

GET my_index/_search
{
  "query": {
    "wildcard": {
      "fieldName": "P*"
    }
  },
  "sort": [
    {
      "_timestamp": {
        "order": "asc"
      }
    }
  ]
}

Note: This _timestamp is deprecated and removed in the latest version of the elasticsearch, if you have any plan in upgrading the elasticsearch then you should add a new field timestamp while indexing and update it and should do the sort on that fields.