I'm having trouble getting a particular Elasticsearch sort to work. I've got a type in my index that has an array of metadata objects. I want to sort based on a particular type of metadata, irregardless of the query that I used to get the results in the first place.
In my case my initial query and my sort subquery are different, and I'm wondering if that makes a difference.
As an example here's excerpts of a bash script I've used to build a test case that shows the problem:
curl -s -u $username:$password -X PUT $hostname/sortdemo
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/_mapping -d
'{
"properties": {
"title": {
"type": "text"
},
"category": {
"type": "text"
},
"tags": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"val": {
"type": "keyword"
}
}
}
}
}'
I then add some "thing"s to the index:
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/1 -d '{
"title": "First",
"category": "active",
"tags":[ {"key":"SortTag","val":"apple"}, {"key":"OtherTag","val":"dog"} ]
}'
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/2 -d '{
"title": "Ignore",
"category": "disabled",
"tags":[ {"key":"SortTag","val":"quince"}, {"key":"OtherTag","val":"octopus"} ]
}'
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/3 -d '{
"title": "Second",
"category": "active",
"tags":[ {"key":"SortTag","val":"banana"}, {"key":"OtherTag","val":"angelfish"} ]
}'
curl -s -u $username:$password -X PUT $hostname/sortdemo/thing/4 -d '{
"title": "Third",
"category": "active",
"tags":[ {"key":"SortTag","val":"cherry"}, {"key":"OtherTag","val":"bear"} ]
}'
and finally I search:
curl -s -u $username:$password -X POST $hostname/sortdemo/_search -d '{
"query": {
"bool": {
"must": [
{ "match": { "category": "active" }}
]
}
},
"sort": [
{
"tags.val": {
"order": "asc",
"nested_filter": {
"term": {
"tags.key": {
"value": "SortTag"
}
}
}
}
}
]
}'
This properly returns the "First", "Second", and "Third" items, and does not return the "Ignore" one b/c it has the wrong category. But, my sort order is not working right. I'm getting "Second", "First", "Third", and I'd expect "First", "Second", "Third". Switching the order from "asc" to "desc" or even changing "SortTag" to "garbageval" in the nested filter makes no change to the search order, so it's as if it's not being used at all.