# Case Insensitive Sort on a Keyword Field in 5.x

**URL:** <https://discuss.elastic.co/t/case-insensitive-sort-on-a-keyword-field-in-5-x/68590>\
**Category:** Elasticsearch\
**Created:** [December 9, 2016, 4:55pm UTC](https://discuss.elastic.co/t/case-insensitive-sort-on-a-keyword-field-in-5-x/68590 "2016-12-09T16:55:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vadim\_Rybak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vadim_rybak/32/13781_2.png) [@Vadim\_Rybak](https://discuss.elastic.co/u/Vadim_Rybak)\
**Post date:** [December 9, 2016, 4:55pm UTC](https://discuss.elastic.co/t/case-insensitive-sort-on-a-keyword-field-in-5-x/68590/1 "2016-12-09T16:55:03Z")

</div>

We are using Elasticsearch 5.  
I have a field `city` using a custom analyzer and the following mapping.

**Analyzer**

```
       "analysis": {
          "analyzer": {
            "lowercase_analyzer": {
              "filter": [
                "standard",
                "lowercase",
                "trim"
              ],
              "type": "custom",
              "tokenizer": "keyword"
            }
}

```

Mapping

```
  "city": {
    "type": "text",
    "analyzer": "lowercase_analyzer"
  }

```

I am doing this so that I can do a case insensitive sort on the city field. Here is an example query that I am trying to run

```
{ 
  "query": {
    "term": {
      "email": {
        "value": "some_email@test.com"
      }
    }
  },
"sort": [
    {
      "city": {
        "order": "desc"
      }
    }
  ]
}

```

Here is the error I am getting:

> "Fielddata is disabled on text fields by default. Set fielddata=true  
> on [city] in order to load fielddata in memory by uninverting the  
> inverted index. Note that this can however use significant memory."

I don't want to turn on `FieldData` and incur a performance hit in Elasticsearch. I would like to have a `Keyword` field that is not case sensitive, so that I can perform more meaningful aggregations and sorts on it. Is there no way to do this?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 9, 2016, 5:10pm UTC](https://discuss.elastic.co/t/case-insensitive-sort-on-a-keyword-field-in-5-x/68590/2 "2016-12-09T17:10:48Z")

</div>

In 5.0 and 5.1 you have to use a type `keyword` but for now you can't analyze the text it contains. See

> <https://github.com/elastic/elasticsearch/issues/18064>

In the meantime, you have to format your document if you don't want to use fielddata and creates a new field out of the first one like `city_sort`.

To do that, you can define an ingest pipeline with a lowercase processor which generates that at index time.  
Note that this will come back as part of the `_source` though.

Not ideal but at least a workaround.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 6, 2017, 5:11pm UTC](https://discuss.elastic.co/t/case-insensitive-sort-on-a-keyword-field-in-5-x/68590/3 "2017-01-06T17:11:03Z")

</div>

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