问题描述    在进行es 多检索 按属性排序的时候,有些索引中不存在 该字段 就会报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 {   "error" : {     "root_cause" : [       {         "type" : "query_shard_exception" ,         "reason" : "No mapping found for [eidentity_qq] in order to sort on" ,         "index_uuid" : "B6kE8KesTSiqB4rdr_BWkg" ,         "index" : "book-20201021"        }     ],     "type" : "search_phase_execution_exception" ,     "reason" : "all shards failed" ,     "phase" : "query" ,     "grouped" : true ,     "failed_shards" : [       {         "shard" : 0 ,         "index" : "book-20201021" ,         "node" : "bfSazPXfRjSI2P_cg5XsEQ" ,         "reason" : {           "type" : "query_shard_exception" ,           "reason" : "No mapping found for [eidentity_qq] in order to sort on" ,           "index_uuid" : "B6kE8KesTSiqB4rdr_BWkg" ,           "index" : "book-20201021"          }       }     ]   },   "status" : 400  } 
解决办法是在检索语句中  添加 “unmapped_type”: “keyword”   :
 1 2 3 4 5 6 7 8 "sort": [     {       "eidentity_qq" : {         "order" : "desc" ,         "unmapped_type" : "keyword"        }     }   ] 
或者Java
1 SortBuilders.fieldSort(sortFied).unmappedType("keyword" ) 
以上就可以解决上面的问题:
我这里还遇到一个问题就 我的mapping 结构比较复杂:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 "eidentity_qq" : {          "properties" : {            "dsr" : {              "type" : "keyword"            },            "endDate" : {              "type" : "date"            },            "geo" : {              "type" : "geo_point"            },            "role" : {              "type" : "keyword"            },            "startDate" : {              "type" : "date"            },            "value" : {              "type" : "text",              "fields" : {                "keyword" : {                  "type" : "keyword",                  "ignore_above" : 5000                }              },              "analyzer" : "ik_max_word"            }          }        } 
在检索的时候 语句如下:
1 2 3 4 5 6 7 8 "sort": [     {       "eidentity_qq.value" : {         "order" : "desc" ,         "unmapped_type" : "keyword"        }     }   ] 
这样还是会报错:
1 2 3 4 5 6 7 8 "error": {     "root_cause": [       {         "type" : "illegal_argument_exception" ,         "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [eidentity_qq.value] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."        }     ] } 
其实这里的 eidentity_qq.value  或者 eidentity_qq.value.keyword  都是不对的,直接用 mapping中的字段名称就可以了