今天运维群里通知生产环境ES查询出现错误 No mapping found for
[DEBUG][o.e.a.s.TransportSearchAction] [qtt-public-es-bje-qtt-public-es-06-c] [lw_analysis_author_week_board_1_201904][4], node[Yg4ArHVlQHKYOv1qS0dR_Q], [P], s[STARTED], a[id=xrL7X6F4QJy9UFuw_-6wnw]: Failed to execute [SearchRequest{searchType=QUERY_THEN_FETCH, indices=[lw_analysis_author_week_board_1_201904], indicesOptions=IndicesOptions[id=38, ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, expand_wildcards_closed=false, allow_alisases_to_multiple_indices=true, forbid_closed_indices=true], types=[platform_205], routing=‘null’, preference=‘null’, requestCache=null, scroll=null, maxConcurrentShardRequests=90, batchedReduceSize=512, preFilterShardSize=128, source={
“from” : 0,
“size” : 30,
“query” : {
“bool” : {
“filter” : [
{
“term” : {
“week” : {
“value” : 201914,
“boost” : 1.0
}
}
}
],
“disable_coord” : false,
“adjust_pure_negative” : true,
“boost” : 1.0
}
},
“sort” : [
{
“avg_pv” : {
“order” : “desc”
}
}
]
}}] lastShard [true]
org.elasticsearch.transport.RemoteTransportException: [indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.index.query.QueryShardException: No mapping found for [avg_pv] in order to sort on
查看对应的index后这个index没有数据 创建的时候是有默认的_default_ mapping的
我用命令行拼凑相关语句查询
curl -XGET http:/IP:PORT/lw_analysis_author_week_board_1_201904/_search?pretty -d ‘{“query”: {“bool”: {“filter”: [{“term”: {“week”: 201914}}]}}, “from”:0, “size”:30, “sort”:[{“avg_pv”:{“order”:“desc”}}]}’
查看资料后 知道原因
ES创建index后,虽然指定了_defult_ mapping但根本没有初始化, 在search查询查询排序时候 sort的字段不知道什么类型 就会导致上述错误
解决办法就是 sort的时候 强制指定 sort 字段类型
“sort”:[{“avg_pv”:{“order”:“desc”, “unmapped_type” : “long”}}]
curl -XGET http://ip:port/lw_analysis_author_week_board_1_201904/_search?pretty -d ‘{“query”: {“bool”: {“filter”: [{“term”: {“week”: 201914}}]}}, “from”:0, “size”:30, “sort”:[{“avg_pv”:{“order”:“desc”, “unmapped_type” : “long”}}]}’
就能解决上述错误
参考资料:
https://stackoverflow.com/questions/17051709/no-mapping-found-for-field-in-order-to-sort-on-in-ElasticSearch
https://my.oschina.net/dabird/blog/1926042