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可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
7月前
|
存储
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【已解决】
161 1
|
8月前
|
索引
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),因此无法直接删除它。为了解决这个问题,你可以按照以下步骤进行操作:
545 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.
356 0
已解决elasticsearch-head启动失败,报Failed at the elasticsearch-head@0.0.0 start script.
|
网络安全
【ES】elasticsearch在启动后出现[transport.netty][solr001] exception caught on transport layer[[[id:0x2d16759]]
elasticsearch在启动后出现[transport.netty][solr001] exception caught on transport layer[[[id:0x2d16759]]
561 0
|
安全
ElasticSearch错误:uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException
ElasticSearch错误:uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException
365 0
|
前端开发
ElasticSearch启动报错汇总,ERROR: [2] bootstrap checks failed
ElasticSearch启动报错汇总,ERROR: [2] bootstrap checks failed
ElasticSearch启动报错汇总,ERROR: [2] bootstrap checks failed
|
2月前
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
60 5
|
3月前
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
253 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
|
4月前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo