Elasticsearch Sorting fields anomaly Elasticsearch Sorting fields anomaly elasticsearch elasticsearch

Elasticsearch Sorting fields anomaly


For sorting you need to use lastName.keyword, that's correct, no need to change anything there.

The reason why van der Mescht and van Breda are before Zwane and Zwezwe is because sorting on strings happens on a lexicographical level, i.e. basically using the ASCII table and uppercase characters happen before lowercase ones, so words are sorted in that same order. But since you're sorting in desc mode, that's exactly the opposite:

  • z...
  • ...
  • van der Mescht
  • ...
  • van Breda
  • ...
  • a...
  • ...
  • Zwezwe
  • ...
  • Zwane
  • ...
  • Z...
  • ...
  • A...

To fix this, what you simply need to do is to add a normalizer to your lastName.keyword field, i.e. change your mapping to this and it will work:

{  "settings": {    "index": {      "analysis": {        "filter": {},        "analyzer": {           ...        },        "tokenizer": {          ...        },        "normalizer": {             <-- add this          "lowersort": {            "type": "custom",            "filter": [              "lowercase"            ]          }        }      }    }  },  "mappings": {    "_doc": {      "properties": {        ...        "lastName": {          "type": "text",          "fields": {            "keyword": {              "type": "keyword",              "normalizer": "lowersort",   <-- add this              "ignore_above": 256            }          }        },        ...      }    }  }}