SRH LABS
SRH LABS

Reputation: 393

Elasticsearch: Sort keywords case insensitively and handle missing keys

We want to sort keywords (can contain alphabets, numbers and special chars) case insensitively and always put the missing and null keys to the end regardless of whether we do ascending or descending sort. What is the best way to do this in ES 5.0? We are exploring script sorting or writing a custom comparator. Please advise. Thanks

Upvotes: 1

Views: 765

Answers (1)

snyderL
snyderL

Reputation: 111

We ran into this same issue recently when trying to figure out why our alphabetical sorting was about 95% correct. We had some keywords stored in CAPS, some lowercase, and some Mixed case. If you are using a keyword field to sort on like they recommend in the documentation you will fall victim to the lexicographical order that elastic uses. This will give you BROWN, Boffey, bailey like their example shows.

They say to get around this you should have a keyword field with a lowercase filter. What the documentation isn't updated to show you is that this new field has to be of type text and it also needs to have fieldataset to true. Otherwise you will get an error telling you that fieldata is disabled. They do this to prevent you from sorting on analyzed fields which can consume a lot of heap, but as far as I know this is the only way to achieve true alphabetical, case-insensitive sorting without the use of scripts.

Upvotes: 1

Related Questions