项目场景:
Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
今天在做项目遇到这个问题,Es那边出现了问题,谷粒商城去Es中查数据的时候,根据品牌id去查询数据报错。
问题描述
{"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}
查询结果出现问题,查看构造语句如下
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" } ] } } } } } } } }
原因分析:
我们可以发现很明显是"type":"query_shard_exception","reason":"failed to create query:这里出现了问题,也就是构造query时出现了问题,意味着在query查询brandId出现问题
单独查询看看
GET /_search { "query": { "bool": { "filter": { "terms": { "brandId": [ "9" ] } } } } }
没问题正常返回结果 ,对比java代码构造的dls语句发现
"term": { "brandId": { "value": [ 9 ], "boost": 1 }
terms写成了term,说明构造语句的代码出现问题,查看代码部分
if (param.getBrandId()!=null&¶m.getBrandId().size()>0){ boolQuery.filter(QueryBuilders.termQuery("brandId",param.getBrandId())); }
不出意外这边写的是QueryBuilders.termQuery()而不是QueryBuilders.termsQuery()
解决方案:
注意在这个地方BrandId为一个数组不是一个值,用term去查询当然会报错,这也是一个失误,排了半个小时才排掉,说明了对Es还是不太熟悉对代码还是不太熟悉需要加强巩固
修改java代码
if (param.getBrandId()!=null&¶m.getBrandId().size()>0){ boolQuery.filter(QueryBuilders.termsQuery("brandId",param.getBrandId())); }
排错成功