Skip to content

Instantly share code, notes, and snippets.

@xrado
Created June 8, 2011 11:45
Show Gist options
  • Save xrado/1014273 to your computer and use it in GitHub Desktop.
Save xrado/1014273 to your computer and use it in GitHub Desktop.
elasticsearch date sort
#mapping
curl -X DELETE http://localhost:9200/test/?pretty=true -d ''
curl -X PUT http://localhost:9200/test/?pretty=true -d ''
curl -X PUT http://localhost:9200/test/test/_mapping?pretty=true -d '
{
"test":{
"properties":{
"cid":{
"type":"string",
"index":"not_analyzed"
},
"model":{
"type":"string",
"index":"not_analyzed"
},
"subject":{
"type":"string"
},
"description":{
"type":"string"
},
"country":{
"type":"string",
"index":"not_analyzed"
},
"location":{
"type":"geo_point"
},
"class":{
"type":"string",
"index":"not_analyzed"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss"
},
"expires":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss",
"null_value":-1
}
}
}
}
'
#data
curl -X PUT http://localhost:9200/test/test/4def4f319c76840c61000000?pretty=true -d '
{
"cid":"4dada80b9c7684747e000000",
"model":"a",
"subject":"subject 1",
"description":"sdfgsdfgsd",
"country":"si",
"location":{
"lat":46.05530166626,
"lon":14.514399528503
},
"class":"74.1",
"date":"2011-06-08 10:30:09",
"expires":null
}
'
curl -X PUT http://localhost:9200/test/test/4def4f319c76840c61000001?pretty=true -d '
{
"cid":"4dada80b9c7684747e000001",
"model":"b",
"subject":"subject 2",
"description":"sdfgsdfgsd",
"country":"si",
"location":{
"lat":47.05530166626,
"lon":15.514399528503
},
"class":"74.1",
"date":"2011-06-08 10:30:09",
"expires":"2011-06-10 10:30:09"
}
'
#sorting
curl -X GET http://localhost:9200/test/test/_search?pretty=true?pretty=true -d '{"sort":{"date":"asc"},"query":{"field":{"_all":"*"}}}'
curl -X GET http://localhost:9200/test/test/_search?pretty=true?pretty=true -d '{"sort":{"expires":"asc"},"query":{"field":{"_all":"*"}}}'
@luddic
Copy link

luddic commented Jun 8, 2011

curl -X GET http://localhost:9200/test/test/_search?pretty=true?pretty=true -d '{"sort":{"expires":"asc"},"query":{"field":{"_all":"*"}}, "fields": ["expires"]}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "test",
"_type" : "test",
"_id" : "4def4f319c76840c61000000",
"_score" : null,
"fields" : {
"expires" : null
},
"sort" : [ -1 ]
}, {
"_index" : "test",
"_type" : "test",
"_id" : "4def4f319c76840c61000001",
"_score" : null,
"fields" : {
"expires" : "2011-06-10 10:30:09"
},
"sort" : [ 1307701809000 ]
} ]
}
}

curl -X GET http://localhost:9200/test/test/_search?pretty=true?pretty=true -d '{"sort":{"expires":"desc"},"query":{"field":{"_all":"*"}}, "fields": ["expires"]}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "test",
"_type" : "test",
"_id" : "4def4f319c76840c61000001",
"_score" : null,
"fields" : {
"expires" : "2011-06-10 10:30:09"
},
"sort" : [ 1307701809000 ]
}, {
"_index" : "test",
"_type" : "test",
"_id" : "4def4f319c76840c61000000",
"_score" : null,
"fields" : {
"expires" : null
},
"sort" : [ -1 ]
} ]
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment