shoutadnan
shoutadnan

Reputation: 1470

Elasticsearch: Sort Alphabetically ignoring Numbers & Special Characters

How to sort a text field alphabetically, ignoring the special characters & numbers? By default, the special characters come first followed by numbers and alphabets.

What I need is alphabets should be sorted and appear first, followed by numbers and special characters. Is that even possible in ES 6.3?

I've tried with the custom analyzer to replace all non-alphabetical characters but it didn't work:

{
  "analysis": {
    "analyzer": {
      "alphabets_analyzer": {
        "tokenizer": "standard",
        "type": "keyword",
        "char_filter": [
          "alphabets_char_filter"
        ]
      }
    },
    "char_filter": {
      "alphabets_char_filter": {
        "type": "pattern_replace",
        "pattern": "[^a-zA-Z\\s\\.]",
        "replacement": ""
      }
    }
  }
}

Upvotes: 2

Views: 1197

Answers (1)

shoutadnan
shoutadnan

Reputation: 1470

I was able to resolve this by the following settings at index level:

{
  "analysis": {
    "analyzer": {
      "alphabetsStringAnalyzer": {
        "tokenizer": "standard",
        "filter": "lowercase",
        "type": "custom",
        "char_filter": [
          "alphabets_char_filter"
        ]
      }
    },
    "char_filter": {
      "alphabets_char_filter": {
        "type": "pattern_replace",
        "pattern": "[^a-zA-Z]",
        "replacement": ""
      }
    }
  }
}

And setting this analyzer in the index mapping as below:

"fullName":{
            "type": "keyword",
            "fields": {
                 "raw": {
                     "type": "text",
                     "analyzer": "alphabetsStringAnalyzer",
                     "fielddata": true
                 }
             }
          }

Upvotes: 1

Related Questions