SOLVED: The answer is at the bottom.
First, I have applied the following code first to ES 2.0.0 and then to a later version. Only the version of 2.0.0 works as expected...
Mapping:
{
"mappings":{
"_default_":{
"date_detection": false,
"dynamic_templates":[
{
"arrays":{
"match":"*_array",
"mapping":{
"type":"nested",
"include_in_root":false
}
}
},
{
"dates":{
"match":"*Date",
"mapping":{
"type":"date",
"format":"strict_date_optional_time"
}
}
}
]
}
}
}
Adding Data in bulk:
{"index":{"_type" : "log", "_id" : "2" } }
{"level_array":{"id":2,"ingestDate":null}}
{"index":{"_type" : "log", "_id" : "3" } }
{"level_array":{"id":3,"ingestDate":"2011-08-04"}}
{"index":{"_type" : "log", "_id" : "4" } }
{"level_array":{"id":4,"ingestDate":"2011-08-04T10:00:00"}}
Search sorted:
{
"_source": true,
"query": {
"match_all": {}
},
"from":0,
"size":10,
"sort" : [{
"level_array.ingestDate" : {
"order" : "desc",
"missing" : "_last"
}
}
]
}
In ES 2.0.0 I get the following results:
{
...
"hits": {
...
"hits": [
{
...
"_source": {
"level_array": {
"id": 4,
"ingestDate": "2011-08-04T10:00:00"
}
},
"sort": [
1312452000000
]
},
{
...
"_source": {
"level_array": {
"id": 3,
"ingestDate": "2011-08-04"
}
},
"sort": [
1312416000000
]
},
{
...
"_source": {
"level_array": {
"id": 2,
"ingestDate": null
}
},
"sort": [
-9223372036854775808
]
}
]
}
}
You can see that the sort is correct, and that there are correct values in the sort result.
If we do the same for a later release, eg 2.3.2 or 5.0.0-alpha1, the search gives the following results:
{
...
"hits": {
...
"hits": [
{
...
"_source": {
"level_array": {
"id": 2,
"ingestDate": null
}
},
"sort": [
-9223372036854775808
]
},
{
...
"_source": {
"level_array": {
"id": 4,
"ingestDate": "2011-08-04T10:00:00"
}
},
"sort": [
-9223372036854775808
]
},
{
...
"_source": {
"level_array": {
"id": 3,
"ingestDate": "2011-08-04"
}
},
"sort": [
-9223372036854775808
]
}
]
}
}
Solution:
{
"_source": true,
"query": {
"match_all": {}
},
"from":0,
"size":10,
"sort" : [{
"level_array.ingestDate" : {
"nested_path" : "level_array",
"order" : "desc",
"missing" : "_last"
}
}
]
}