elasticsearch/elasticsearch — Issue #1737: sorting failed when same name under different types with different mapping
Author: medcl
Created: 2012-02-24 16:39:50+00:00
State: open

if there are same fieldname with different data type in different type,
for example,datetime and string,
type_a have field:time,data type is date
type_b have field:time,data type is string,(this type may be created dynamic)

if i want to query against type_a use this query:
http://localhost:9200/index/type_a/_search?q=*&sort=time

perhaps,you may get this error(not always,occasional after a restart)

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[Pqdw_LAFSbOfyo9yVU9aaw][index][0]: QueryPhaseExecutionException[[index][0]: query[ConstantScore(*:*)],from[0],size[10],sort[]: 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]; }{[Pqdw_LAFSbOfyo9yVU9aaw][index][1]: QueryPhaseExecutionException[[index][1]: query[ConstantScore(*:*)],from[0],size[10],sort[]: 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]; }{[Pqdw_LAFSbOfyo9yVU9aaw][xxx][2]: QueryPhaseExecutionException[[index][2]: query[ConstantScore(*:*)],from[0],size[10],sort[]: 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]; }]","status":500}

this line:
https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/search/sort/SortParseElement.java#L159
it returned the wrong FieldMapper,it should be "LongFieldMapper" but actually returned is :"StringFieldMapper",did'nt load the right data,so sorting failed ,
with the given parameter "fieldName",it didn't know which mapping will be used,and elasticsearch should be more smart,to choose the right mapping,in this case,if we have explicit specify the type(in url:http://localhost:9200/index/type_a/_search),es can select the mapping within type:type_a.