ES 查询时提示:all shards failed [type=search_phase_execution_exception]

ES 查询时提示:all shards failed [type=search_phase_execution_exception],第1张

我的情况和解决方案

这种错误大概率是 ES 的查询语句语法错误,比如我当时是,时间筛选的条件的格式写错了,导致 ES 查询时解析错误,从而报了

all shards failed [type=search_phase_execution_exception]

这个错误

ES 查询时提示:all shards failed [type=search_phase_execution_exception],第2张

排查思路:先想办法把查询 DSL 语句打印出来,看下这个最终用来查询的 DSL 语句在语法上有没有问题,如果有问题就改正。

网上对于这个错误还有一些其他的原因,这里简单复制过来,方便以后遇到相同问题时查询使用

其他网友的情况和解决方案

当使用到term 查询的时候,由于是精准匹配,所以查询的关键字在 es 上的类型,必须是 keyword 而不能是 text,比如你的搜索条件是 “name”:”jack”,那么该 name 字段的 es 类型得是keyword,而不能是 text。比如下面是一个 DSL语句的例子,

ES 查询时提示:all shards failed [type=search_phase_execution_exception],第3张

解决方案一是修改 ES 的 mapping,把需要精准匹配的字段设置成 keyword 类型,

方案二是修改 DSL,让查询的字段类型按照 keyword 的方式来查询,query 里增加

.keyword

后缀。但是这种方案我都没有亲自实验过,不知道是否能真的解决问题,下次遇到了相同问题再来检验一下。

修改 DSL 中的语句,字段里增加

.keyword

后缀。

ES 查询时提示:all shards failed [type=search_phase_execution_exception],第4张

在我们的Java 或者 Golang 代码中怎么修改呢?如下,加上".keyword"就可以了

Java 客户端

ES 查询时提示:all shards failed [type=search_phase_execution_exception],第5张

Golang 客户端

ES 查询时提示:all shards failed [type=search_phase_execution_exception],第6张

附录(如何打印输出 DSL语句)

// 创建MatchAll查询,这里替换成你自己的 elasticClient 对象 matchAllQuery := elasticClient.NewMatchAllQuery() // 获取查询的JSON表示(DSL语句),这里需要替换成你自己的 query 对象 querySrc, err := matchAllQuery.Source() if err != nil { log.Fatalf("Error getting query source: %s", err) } // 序列化查询对象 queryStr, err := json.MarshalIndent(querySrc, "", " ") if err != nil { log.Fatalf("Error marshalling query to JSON: %s", err) } // 打印DSL语句 fmt.Println("DSL Query:\n", string(queryStr))

参考已解决:Elasticsearch报错:exception [type=search_phase_execution_exception, reason=all shards failed]all shards failed [type=search_phase_execution_exception]

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/13518615.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024-02-09
下一篇 2024-02-14

发表评论

登录后才能评论

评论列表(0条)

保存