# Sort strange behaviour

**URL:** <https://discuss.elastic.co/t/sort-strange-behaviour/7155>\
**Category:** Elasticsearch\
**Created:** [March 27, 2012, 10:31pm UTC](https://discuss.elastic.co/t/sort-strange-behaviour/7155 "2012-03-27T22:31:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lizal](https://avatars.discourse-cdn.com/v4/letter/l/4af34b/32.png) [@lizal](https://discuss.elastic.co/u/lizal)\
**Post date:** [March 27, 2012, 10:31pm UTC](https://discuss.elastic.co/t/sort-strange-behaviour/7155/1 "2012-03-27T22:31:03Z")

</div>

I've mapped airports

airport: {  
properties: {  
iataCode: {type: string},  
name: {type: string},  
country: {type: string},  
city: {type: string},  
}  
}

if i sort by iataCode (unique key) there's no problem, but i have an  
exception trying to sort by city (for example New York have several  
airports).

My code:  
SearchResponse response = client.prepareSearch("geo")  
.setSearchType(SearchType.QUERY\_AND\_FETCH)  
.setQuery(qb1)  
.addSort("city", SortOrder.ASC)  
.setSize(5000)  
.execute()  
.actionGet();

The exception:  
Exception in thread "main"  
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed  
to execute phase [query\_fetch], total failure; shardFailures  
{[Fil2OCNpR3qOrlTwEK5\_Pw][geo][2]: RemoteTransportException[[Zabu]  
[inet[/192.168.2.34:9301]][search/phase/query+fetch]]; nested:  
QueryPhaseExecutionException[[geo][2]:  
query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":  
org.elasticsearch.index.field.data.strings.StringFieldDataType  
$1@1ce67ca\>]: Query Failed [Failed to execute main query]]; nested:  
IOException[Can't sort on string types with more than one value per  
doc, or more than one token per field]; }{[Fil2OCNpR3qOrlTwEK5\_Pw][geo]  
[4]: RemoteTransportException[[Zabu][inet[/192.168.2.34:9301]][search/  
phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][4]:  
query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":  
org.elasticsearch.index.field.data.strings.StringFieldDataType  
$1@918c34\>]: Query Failed [Failed to execute main query]]; nested:  
IOException[Can't sort on string types with more than one value per  
doc, or more than one token per field]; }{[EV812JrLQZSjEk9rp9nP2Q][geo]  
[1]: RemoteTransportException[[Feral][inet[/192.168.2.34:9300]][search/  
phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][1]:  
query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":  
org.elasticsearch.index.field.data.strings.StringFieldDataType  
$1@2af131\>]: Query Failed [Failed to execute main query]]; nested:  
IOException[Can't sort on string types with more than one value per  
doc, or more than one token per field]; }{[Fil2OCNpR3qOrlTwEK5\_Pw][geo]  
[0]: RemoteTransportException[[Zabu][inet[/192.168.2.34:9301]][search/  
phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][0]:  
query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":  
org.elasticsearch.index.field.data.strings.StringFieldDataType  
$1@71c619\>]: Query Failed [Failed to execute main query]]; nested:  
IOException[Can't sort on string types with more than one value per  
doc, or more than one token per field]; }{[EV812JrLQZSjEk9rp9nP2Q][geo]  
[3]: RemoteTransportException[[Feral][inet[/192.168.2.34:9300]][search/  
phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][3]:  
query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":  
org.elasticsearch.index.field.data.strings.StringFieldDataType  
$1@18b0fd\>]: Query Failed [Failed to execute main query]]; nested:  
IOException[Can't sort on string types with more than one value per  
doc, or more than one token per field]; }  
at org.elasticsearch.action.search.type.TransportSearchTypeAction  
$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:  
259)  
at org.elasticsearch.action.search.type.TransportSearchTypeAction  
$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:212)  
at org.elasticsearch.search.action.SearchServiceTransportAction  
$5.handleException(SearchServiceTransportAction.java:262)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.handleException(MessageChannelHandler.java:  
288)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:  
279)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.process(MessageChannelHandler.java:  
231)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.callDecode(MessageChannelHandler.java:  
141)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:  
93)  
at  
org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:  
75)  
at  
org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:  
558)  
at  
org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:  
553)  
at  
org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:  
268)  
at  
org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:  
255)  
at  
org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:  
343)  
at  
org.elasticsearch.common.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:  
274)  
at  
org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:  
194)  
at  
org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:  
102)  
at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker  
$1.run(DeadLockProofWorker.java:42)  
at java.util.concurrent.ThreadPoolExecutor  
$Worker.runTask(ThreadPoolExecutor.java:886)  
at java.util.concurrent.ThreadPoolExecutor  
$Worker.run(ThreadPoolExecutor.java:908)  
at java.lang.Thread.run(Thread.java:662)

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 27, 2012, 11:14pm UTC](https://discuss.elastic.co/t/sort-strange-behaviour/7155/2 "2012-03-27T23:14:44Z")

</div>

By default, string fields are analyzed. It means that the field "city" may  
contain multiple tokens. For example, "New York" generates two tokens "new"  
and "york". Elasticsearch can only sort by fields that contain a single  
token. You can change your mapping to "city": { "type": "string", "index":  
"not\_analyzed"} to fix this problem.

On Tuesday, March 27, 2012 6:31:03 PM UTC-4, lizal wrote:

> I've mapped airports
> 
> airport: {  
> properties: {  
> iataCode: {type: string},  
> name: {type: string},  
> country: {type: string},  
> city: {type: string},  
> }  
> }
> 
> if i sort by iataCode (unique key) there's no problem, but i have an  
> exception trying to sort by city (for example New York have several  
> airports).
> 
> My code:  
> SearchResponse response = client.prepareSearch("geo")  
> .setSearchType(SearchType.QUERY\_AND\_FETCH)  
> .setQuery(qb1)  
> .addSort("city", SortOrder.ASC)  
> .setSize(5000)  
> .execute()  
> .actionGet();
> 
> The exception:  
> Exception in thread "main"  
> org.elasticsearch.action.search.SearchPhaseExecutionException: Failed  
> to execute phase [query\_fetch], total failure; shardFailures  
> {[Fil2OCNpR3qOrlTwEK5\_Pw][geo][2]: RemoteTransportException[[Zabu]  
> [inet[/192.168.2.34:9301]][search/phase/query+fetch]]; nested:  
> QueryPhaseExecutionException[[geo][2]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":
> 
> org.elasticsearch.index.field.data.strings.StringFieldDataType  
> $1@1ce67ca\>]: Query Failed [Failed to execute main query]]; nested:  
> IOException[Can't sort on string types with more than one value per  
> doc, or more than one token per field]; }{[Fil2OCNpR3qOrlTwEK5\_Pw][geo]  
> [4]: RemoteTransportException[[Zabu][inet[/192.168.2.34:9301]][search/  
> phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][4]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":
> 
> org.elasticsearch.index.field.data.strings.StringFieldDataType  
> $1@918c34\>]: Query Failed [Failed to execute main query]]; nested:  
> IOException[Can't sort on string types with more than one value per  
> doc, or more than one token per field]; }{[EV812JrLQZSjEk9rp9nP2Q][geo]  
> [1]: RemoteTransportException[[Feral][inet[/192.168.2.34:9300]][search/  
> phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][1]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":
> 
> org.elasticsearch.index.field.data.strings.StringFieldDataType  
> $1@2af131\>]: Query Failed [Failed to execute main query]]; nested:  
> IOException[Can't sort on string types with more than one value per  
> doc, or more than one token per field]; }{[Fil2OCNpR3qOrlTwEK5\_Pw][geo]  
> [0]: RemoteTransportException[[Zabu][inet[/192.168.2.34:9301]][search/  
> phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][0]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":
> 
> org.elasticsearch.index.field.data.strings.StringFieldDataType  
> $1@71c619\>]: Query Failed [Failed to execute main query]]; nested:  
> IOException[Can't sort on string types with more than one value per  
> doc, or more than one token per field]; }{[EV812JrLQZSjEk9rp9nP2Q][geo]  
> [3]: RemoteTransportException[[Feral][inet[/192.168.2.34:9300]][search/  
> phase/query+fetch]]; nested: QueryPhaseExecutionException[[geo][3]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[5000],sort[\<custom:"city":
> 
> org.elasticsearch.index.field.data.strings.StringFieldDataType  
> $1@18b0fd\>]: Query Failed [Failed to execute main query]]; nested:  
> IOException[Can't sort on string types with more than one value per  
> doc, or more than one token per field]; }  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction  
> $BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:  
> 259)  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction  
> $BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:212)  
> at org.elasticsearch.search.action.SearchServiceTransportAction  
> $5.handleException(SearchServiceTransportAction.java:262)  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler.handleException(MessageChannelHandler.java:
> 
> 1. at  
> org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:
> 
> 2. at  
> org.elasticsearch.transport.netty.MessageChannelHandler.process(MessageChannelHandler.java:
> 
> 3. at  
> org.elasticsearch.transport.netty.MessageChannelHandler.callDecode(MessageChannelHandler.java:
> 
> 4. at  
> org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:
> 
> 5. 
> 
> ```
> at 
> 
> ```
> 
> org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:
> 
> 1. 
> 
> ```
> at 
> 
> ```
> 
> org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:
> 
> 1. at  
> org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:
> 
> 2. at  
> org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:
> 
> 3. at  
> org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:
> 
> 4. at  
> org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:
> 
> 5. at  
> org.elasticsearch.common.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:
> 
> 6. at  
> org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:
> 
> 7. at  
> org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:
> 
> 8. at  
> org.elasticsearch.common.netty.util.internal.DeadLockProofWorker  
> $1.run(DeadLockProofWorker.java:42)  
> at java.util.concurrent.ThreadPoolExecutor  
> $Worker.runTask(ThreadPoolExecutor.java:886)  
> at java.util.concurrent.ThreadPoolExecutor  
> $Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:662)

---

<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:** [July 6, 2017, 3:34am UTC](https://discuss.elastic.co/t/sort-strange-behaviour/7155/3 "2017-07-06T03:34:31Z")

</div>


