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
+24.6k Golang : Change file read or write permission example
+37.8k Golang : Comparing date or timestamp
+7.1k Golang : Find the shortest line of text example
+8.3k Golang : Qt splash screen with delay example
+9.5k Golang : How to protect your source code from client, hosting company or hacker?
+8k Golang Hello World Example
+12.4k Golang : Get remaining text such as id or filename after last segment in URL path
+16.1k Golang : How to reverse elements order in map ?
+7.4k Golang : How to convert strange string to JSON with json.MarshalIndent
+13.4k Golang : Date and Time formatting
+21.8k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+5.3k Golang : Calculate half life decay example