Elastic Search : Mapping date format and sort by date
While working on the job board for Socketloop.com recently, I need to sort the Elastic Search result based on the date when the entry is created.
Before the sorting part can happen, first the mappings need to be configured with the type "date" with a proper formatting.
"postDate": {"type": "date", "format" : "yyyy-MM-dd HH:mm:ss"}
and below is a snippet from the Elastic Search mapping.
"mappings" : {
"people": {
"properties": {
"username": {"type": "string"},
"postDate": {"type": "date", "format" : "yyyy-MM-dd HH:mm:ss"}
}
}
and add the 'sort' section in my Elastic Search query
$searchquery = '{
"size" : 9999,
"sort" : [
{ "postDate" : {"order" : "desc"}}
]
}';
and the result be seen at https://profession.socketloop.com/people (still pretty new and not many records to sort for the moment - March 2015)
Reference :
http://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
See also : PHP : How to parse ElasticSearch JSON ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.7k Golang : Output or print out JSON stream/encoded data
+13k Golang : Find network of an IP address
+6.2k Golang : File system scanning
+5.5k Nginx : Password protect a directory/folder
+8.5k Golang : Eroding and dilating image with OpenCV example
+12.8k Golang : ROT47 (Caesar cipher by 47 characters) example
+12k Golang : Convert(cast) int to int64
+17.8k Golang : Get current URL example
+10.7k Golang : Convert a rune to unicode style string \u
+8.8k Golang : Convert octal value to string to deal with leading zero problem
+16.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+20.7k Golang : Securing password with salt