# Is sorting on Text/String field no longer available in 5.x?

**URL:** <https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586>\
**Category:** Elasticsearch\
**Created:** [December 9, 2016, 4:21pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586 "2016-12-09T16:21:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![juminoz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/juminoz/32/13110_2.png) [@juminoz](https://discuss.elastic.co/u/juminoz)\
**Post date:** [December 9, 2016, 4:21pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/1 "2016-12-09T16:21:41Z")

</div>

I understand that this is a new change, but I can't see what's wrong with this mapping. This is taken from the mapping of the field registered in ElasticSearch through \_mapping call:

```
"key": {
    "type": "text",
    "fields": {
        "keyword": {
             "type": "keyword",
             "ignore_above": 256
        }
    }
}

```

When I sort using the "key" field, I get this exception. I have also tried adding fieldData=true, but that also didn't work.

```
Caused by: RemoteTransportException[[_6qwpaI][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [key] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.];
Caused by: java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [key] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.
	at org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:335)
	at org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:111)
	at org.elasticsearch.index.query.QueryShardContext.getForField(QueryShardContext.java:167)
	at org.elasticsearch.search.sort.FieldSortBuilder.build(FieldSortBuilder.java:281)
	at org.elasticsearch.search.sort.SortBuilder.buildSort(SortBuilder.java:151)
	at org.elasticsearch.search.SearchService.parseSource(SearchService.java:678)
	at org.elasticsearch.search.SearchService.createContext(SearchService.java:536)
	at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:502)
	at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:243)
	at org.elasticsearch.action.search.SearchTransportService.lambda$registerRequestHandler$6(SearchTransportService.java:276)
	at org.elasticsearch.transport.TransportRequestHandler.messageReceived(TransportRequestHandler.java:33)
	at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:69)
	at org.elasticsearch.transport.TransportService$6.doRun(TransportService.java:550)
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:527)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

```

At first I thought this might be related to the use of a field (e.g. key.keyword), but since I'm not doing that, then I don't see any reason why this is not working.

I'm pretty new to ElasticSearch 5.x and the document is contradicting itself so I hope someone can point me to the right direction. This is what I referring to:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html)

I'm essentially trying to achieve the same as this original mapping that was working until 5.x:

```
"mapping": {
    "type": "string",
    "fields": {
        "raw": {
            "type": "string",
            "ignore_above": 256
        },
        "english": {
            "type": "string",
            "analyzer": "english"
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [December 9, 2016, 5:25pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/2 "2016-12-09T17:25:47Z")

</div>

> [@juminoz](#):
>
> at first I thought this might be related to the use of a field (e.g. key.keyword), but since I'm not doing that, then I don't see any reason why this is not working.

This is what you need to do though, instead of sorting on `key`, sort on `key.keyword`, which will use the non-analyzed version (that has doc\_values) for sorting.

---

<div class="post-metadata">

**Author:** ![juminoz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/juminoz/32/13110_2.png) [@juminoz](https://discuss.elastic.co/u/juminoz)\
**Post date:** [December 9, 2016, 5:32pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/3 "2016-12-09T17:32:44Z")

</div>

I switched to sort by keyword and that sorted out the issue. Is this field now autogenerated? It got created without me specifying anything with field name "keyword". As for the fields I specified, they just don't get added in the mapping. This is my mapping:

```
{
                "strings": {
                    "match_mapping_type": "text",
                    "mapping": {
                        "type": "text",
                        "fields": {
                            "raw": {
                                "type": "keyword",
                                "ignore_above": 256
                            },
                            "english": {
                                "type": "text",
                                "analyzer": "english"
                            }
                        }
                    }
                }
            }
```

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [December 9, 2016, 5:46pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/4 "2016-12-09T17:46:21Z")

</div>

> [@juminoz](#):
>
> Is this field now autogenerated?

If you index a string with no mapping, ES in 5.0+ now automatically creates a `text` version and a `keyword` version (under `.keyword`) of the field.

---

<div class="post-metadata">

**Author:** ![juminoz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/juminoz/32/13110_2.png) [@juminoz](https://discuss.elastic.co/u/juminoz)\
**Post date:** [December 9, 2016, 5:49pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/5 "2016-12-09T17:49:23Z")

</div>

But I have mapping as default template, but it's not honoring the mapping in the template. I guess that's another breaking change between 2.x to 5.x? This is a major issue.

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [December 9, 2016, 5:56pm UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/6 "2016-12-09T17:56:11Z")

</div>

> [@juminoz](#):
>
> But I have mapping as default template, but it's not honoring the mapping in the template.

This is because you have a typo in your dynamic mapping configuration, you are trying to match fields of type "text", but it should be "string" instead. For example, this works:

```
 PUT /test?pretty
 {
   "mappings": {
     "doc": {
       "dynamic_templates": [
         {
           "strings": {
             "match_mapping_type": "string",
             "mapping": {
               "type": "text",
               "fields": {
                 "raw": {
                   "type": "keyword",
                   "ignore_above": 42
                 },
                 "english": {
                   "type": "text",
                   "analyzer": "english"
                 }
               }
             }
           }
         }
       ]
     }
   }
 }

 POST /test/doc/1
 {"body": "foo"}

 GET /test/doc/_mapping?pretty

```

---

<div class="post-metadata">

**Author:** ![juminoz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/juminoz/32/13110_2.png) [@juminoz](https://discuss.elastic.co/u/juminoz)\
**Post date:** [December 10, 2016, 12:12am UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/7 "2016-12-10T00:12:32Z")

</div>

I thought there is no longer type "string" in 5.x? That's why I changed it to "text". So to make it work, I need to continue using string? That doesn't sound right.

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [December 10, 2016, 3:09am UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/8 "2016-12-10T03:09:09Z")

</div>

> [@juminoz](#):
>
> I thought there is no longer type "string" in 5.x? That's why I changed it to "text". So to make it work, I need to continue using string? That doesn't sound right.

There's a disconnect for this, the "dynamic" type used for `match_mapping_type` is the type of the field, not necessarily the ES type. For instance, `match_mapping_type` only supports "long", not "integer" because it maps to the data type rather than an ES type. So even though ES itself uses "text" and "keyword", the data type is still a "string".

I agree this is confusing, there was a PR here: [Elasticsearch should reject dynamic templates with unknown `match_mapping_type`. by jpountz · Pull Request #17285 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/pull/17285) for 5.0+ that adds deprecation logging for this, and I opened [Throw an exception on unrecognized "match\_mapping\_type" by dakrone · Pull Request #22090 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/pull/22090) so 6.0 will throw an exception when an unrecognized type is used.

---

<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 7, 2017, 3:09am UTC](https://discuss.elastic.co/t/is-sorting-on-text-string-field-no-longer-available-in-5-x/68586/9 "2017-01-07T03:09:26Z")

</div>

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