項(xiàng)目場(chǎng)景:
Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
今天在做項(xiàng)目遇到這個(gè)問(wèn)題,Es那邊出現(xiàn)了問(wèn)題,谷粒商城去Es中查數(shù)據(jù)的時(shí)候,根據(jù)品牌id去查詢數(shù)據(jù)報(bào)錯(cuò)。
?
問(wèn)題描述
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: {\n ?\"bool\" : {\n ? ?\"filter\" : [\n ? ? ?{\n ? ? ? ?\"term\" : {\n ? ? ? ? ?\"brandId\" : {\n ? ? ? ? ? ?\"value\" : \"]\",\n ? ? ? ? ? ?\"boost\" : 1.0\n ? ? ? ? ?}\n ? ? ? ?}\n ? ? ?}\n ? ?],\n ? ?\"adjust_pure_negative\" : true,\n ? ?\"boost\" : 1.0\n ?}\n}","index_uuid":"Ihj_ANPsQAOj8Lg3lnCdVA","index":"gulimall_product"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"gulimall_product","node":"lIkiIqcQSwSzRMIbnLDsYA","reason":{"type":"query_shard_exception","reason":"failed to create query: {\n ?\"bool\" : {\n ? ?\"filter\" : [\n ? ? ?{\n ? ? ? ?\"term\" : {\n ? ? ? ? ?\"brandId\" : {\n ? ? ? ? ? ?\"value\" : \"]\",\n ? ? ? ? ? ?\"boost\" : 1.0\n ? ? ? ? ?}\n ? ? ? ?}\n ? ? ?}\n ? ?],\n ? ?\"adjust_pure_negative\" : true,\n ? ?\"boost\" : 1.0\n ?}\n}","index_uuid":"Ihj_ANPsQAOj8Lg3lnCdVA","index":"gulimall_product","caused_by":{"type":"number_format_exception","reason":"For input string: \"]\""}}}]},"status":400}
查詢結(jié)果出現(xiàn)問(wèn)題,查看構(gòu)造語(yǔ)句如下
GET gulimall_product/_search
{
"from": 0,
"size": 16,
"query": {
"bool": {
"filter": [
{
"term": {
"brandId": {
"value": [
9
],
"boost": 1
}
},
"terms": {
"brandId": [
"1",
"9"
]
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"aggregations": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 50,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"brand_name_agg": {
"terms": {
"field": "brandName",
"size": 1,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"brand_img_agg": {
"terms": {
"field": "brandImg",
"size": 1,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
},
"catalog_agg": {
"terms": {
"field": "catalogId",
"size": 20,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"catalog_name_agg": {
"terms": {
"field": "catalogName",
"size": 1,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
},
"attr_agg": {
"nested": {
"path": "attrs"
},
"aggregations": {
"attr_id_agg": {
"terms": {
"field": "attrs.attrId",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"attr_name_agg": {
"terms": {
"field": "attrs.attrName",
"size": 1,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"attr_value_agg": {
"terms": {
"field": "attrs.attrValue",
"size": 50,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
}
}
}
}
?
原因分析:
我們可以發(fā)現(xiàn)很明顯是"type":"query_shard_exception","reason":"failed to create query:這里出現(xiàn)了問(wèn)題,也就是構(gòu)造query時(shí)出現(xiàn)了問(wèn)題,意味著在query查詢brandId出現(xiàn)問(wèn)題
單獨(dú)查詢看看
GET /_search
{
"query": {
"bool": {
"filter": {
"terms": {
"brandId": [
"9"
]
}
}
}
}
}
沒問(wèn)題正常返回結(jié)果?,對(duì)比java代碼構(gòu)造的dls語(yǔ)句發(fā)現(xiàn)
"term": { "brandId": { "value": [ 9 ], "boost": 1 }
terms寫成了term,說(shuō)明構(gòu)造語(yǔ)句的代碼出現(xiàn)問(wèn)題,查看代碼部分
if (param.getBrandId()!=null&¶m.getBrandId().size()>0){ boolQuery.filter(QueryBuilders.termQuery("brandId",param.getBrandId())); }
不出意外這邊寫的是QueryBuilders.termQuery()而不是QueryBuilders.termsQuery()
解決方案:
注意在這個(gè)地方BrandId為一個(gè)數(shù)組不是一個(gè)值,用term去查詢當(dāng)然會(huì)報(bào)錯(cuò),這也是一個(gè)失誤,排了半個(gè)小時(shí)才排掉,說(shuō)明了對(duì)Es還是不太熟悉對(duì)代碼還是不太熟悉需要加強(qiáng)鞏固
修改java代碼
if (param.getBrandId()!=null&¶m.getBrandId().size()>0){ boolQuery.filter(QueryBuilders.termsQuery("brandId",param.getBrandId())); }
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-434591.html
排錯(cuò)成功?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-434591.html
到了這里,關(guān)于Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!