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
+23.8k Golang : Call function from another package
+11.9k Golang : Save webcamera frames to video file
+5.3k Golang : Get S3 or CloudFront object or file information
+10.3k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+22.7k Golang : Test file read write permission example
+8k Golang : How To Use Panic and Recover
+19.7k Golang : How to get time from unix nano example
+13.9k Golang : Reverse IP address for reverse DNS lookup example
+3.5k Java : Get FX sentiment from website example
+5.6k Golang : Struct field tags and what is their purpose?
+8.8k Golang : automatically figure out array length(size) with three dots
+10.1k Golang : How to check if a website is served via HTTPS