Yauheni Pozdnyakov
Yauheni Pozdnyakov

Reputation: 203

Alphabetical sort using elasticsearch

I wounder if there any way or setting to perform an alphabetical sort in elasticsearch. I've got a field and I want to perform sort in descending order over it. Elastic performs it lexicographically. What I get:

Company name
Customer name
company address

What I want to get:

Company name
company address
Customer name

I found that I can create a custom analyser, but maybe there can be a better option?

Upvotes: 2

Views: 6895

Answers (1)

user3775217
user3775217

Reputation: 4803

use multifields to index the text field as lowercased keyword with fielddata true where you can sort.

{
  "settings": {
    "analysis": {
      "analyzer": {
        "keyword_lowercase": {
          "tokenizer": "keyword",
          "filter": ["lowercase"]
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "properties": {
        "text": { 
          "type": "text",
          "fields": {
            "raw": { 
              "type":     "text",
              "analyzer": "keyword_lowercase",
              "fielddata": true
            }
          }
        }
      }
    }
  }
}

Query

{
  "sort": [
    {
      "text.raw": {
        "order": "asc"
      }
    }
  ]
}

Upvotes: 1

Related Questions