# Sorting on keyword not working as expected

I am storing Windows UNC paths in a path field with the following mapping:

"path": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
},
"tree": {
"type": "text",
"analyzer": "custom_path_tree"
}
}
}


When I run this query:

{
"sort": [
{
"fs.path.keyword": {
"order": "desc"
}
}
],
"_source": "path",
"size": 1
}


I get a document with a path like \\foo\bar\FN1\FFF. However, looking at the other documents in the index, I see others with a path \\foo\bar\FN15\EEE.

I would have thought \ would come before 5 when sorting against a keyword field.

Why is it doing this and how can I get Elasticsearch to sort this field so that \\foo\bar\FN15\EEE comes after \\foo\bar\FN1\FFF?

You're using descending order and \ comes before 5 in that order - e.g. like reading this table backwards

A common technique when sorting numbers as string is to zero pad appropriately e.g. use FN001 instead of FN1. I doubt that's a good answer for examining existing file names but if that's the sort logic you need then that's how you'll have to prep your content

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.