Sort strange behaviour


(lizal) #1

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)


(Igor Motov) #2

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)


(system) #3