Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]

项目场景:


Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]


今天在做项目遇到这个问题,Es那边出现了问题,谷粒商城去Es中查数据的时候,根据品牌id去查询数据报错。


79a3694d813f4d3b8ad505149be31f26.png

6941d9223acc47629197a47fd3c90372.png


问题描述


{"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&&param.getBrandId().size()>0){
            boolQuery.filter(QueryBuilders.termQuery("brandId",param.getBrandId()));
        }

不出意外这边写的是QueryBuilders.termQuery()而不是QueryBuilders.termsQuery()


解决方案:


注意在这个地方BrandId为一个数组不是一个值,用term去查询当然会报错,这也是一个失误,排了半个小时才排掉,说明了对Es还是不太熟悉对代码还是不太熟悉需要加强巩固

修改java代码

if (param.getBrandId()!=null&&param.getBrandId().size()>0){
    boolQuery.filter(QueryBuilders.termsQuery("brandId",param.getBrandId()));
}

bbc408687cfd47479de7be54a11b6c89.png

排错成功

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
9天前
|
存储
Elasticsearch exception [type=cluster_block_exception, reason=blocked by: [FORBIDDEN/12/index r【已解决】
Elasticsearch exception [type=cluster_block_exception, reason=blocked by: [FORBIDDEN/12/index r【已解决】
11 1
|
1月前
|
索引
Elasticsearch exception [type=illegal_argument_exception, reason=index [.1] is the write index for data stream [slowlog] and cannot be deleted]
在 Elasticsearch 中,你尝试删除的索引是一个数据流(data stream)的一部分,而且是数据流的写入索引(write index),因此无法直接删除它。为了解决这个问题,你可以按照以下步骤进行操作:
|
网络安全
【ES】elasticsearch在启动后出现[transport.netty][solr001] exception caught on transport layer[[[id:0x2d16759]]
elasticsearch在启动后出现[transport.netty][solr001] exception caught on transport layer[[[id:0x2d16759]]
421 0
|
数据可视化
已解决elasticsearch-head启动失败,报Failed at the elasticsearch-head@0.0.0 start script.
已解决elasticsearch-head启动失败,报Failed at the elasticsearch-head@0.0.0 start script.
292 0
已解决elasticsearch-head启动失败,报Failed at the elasticsearch-head@0.0.0 start script.
|
安全
ElasticSearch错误:uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException
ElasticSearch错误:uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException
260 0
|
前端开发
ElasticSearch启动报错汇总,ERROR: [2] bootstrap checks failed
ElasticSearch启动报错汇总,ERROR: [2] bootstrap checks failed
ElasticSearch启动报错汇总,ERROR: [2] bootstrap checks failed
|
安全
运行ElasticSearch报错:uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException
运行ElasticSearch报错:uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException
955 0
|
存储 JSON 自然语言处理
Elasticsearch Document & Type & Index
要了解Eleasticsearch,首先就要从基本的对象进行了解,这一篇是介绍什么是文档(Document),什么是类型(Document Type),什么是索引(Index),如何去创建和使用。
383 0
Elasticsearch Document & Type & Index
|
编解码 消息中间件 Kafka
filebeat-kafka-logstash-elasticsearch日志收集[中文时区问题、自己创建type类index]
logstash配置文件如下: 点击(此处)折叠或打开 input{     kafka{         bootstrap_servers => "xxxx:9092,xxxx:9092,dw72.
2357 0