已解决:Elasticsearch报错:exception [type=search_phase_execution_exception, reason=all shards failed]
-
Exception in thread "main" ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default. Set fielddata=true on [content_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default. Set fielddata=true on [content_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.]];
-
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
-
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1727)
-
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1704)
-
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1467)
-
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424)
-
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394)
-
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:930)
-
at com.softsec.util.demoTime.main(demoTime.java:98)
-
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://192.168.101.92:9200], URI [/news/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 400 Bad Request]
-
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [content_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"news","node":"8GuMfo5aRz2CCgl49bY0aQ","reason":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [content_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}],"caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [content_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.","caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [content_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}},"status":400}
-
at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:253)
-
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:231)
-
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205)
-
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1454)
-
... 4 more
这个原因是因为我的分组聚合查询的字符串(content_type)类型是text类型
原因分析:
当使用到term 查询的时候,由于是精准匹配,所以查询的关键字在es上的类型,必须是keyword而不能是text,比如你的搜索条件是 ”name”:”蔡虚坤”,那么该name 字段的es类型得是keyword,而不能是text
在es中,只有keyword类型的字符串可以使用AggregationBuilders.terms("aggs-class")来分组聚合,想要分组查询,指定根据分组字段的keyword属性就可以了(如下图);
在我们的Java代码中怎么修改呢?如下,加上".keyword"就可以了
之前的报错下面追加上:
分片配置
•在创建索引时,如果不指定分片配置,则默认主分片1,副本分片1。
•在创建索引时,可以通过settings设置分片
分片配置
#分片配置
#"number_of_shards": 3, 主分片数量
#"number_of_replicas": 1 主分片备份数量,每一个主分片有一个备份
# 3个主分片+3个副分片=6个分片
PUT cluster_test1
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name":{
"type": "text"
}
}
}
}
1.三个节点正常运行(0、1、2分片标号)
2.cluster-3 挂掉
3.将挂掉节点的分片,自平衡到其他节点
4.cluster-3 恢复正常后,节点分片将自平衡回去(并不一定是原来的分片)
分片与自平衡
•当节点挂掉后,挂掉的节点分片会自平衡到其他节点中
注意:分片数量一旦确定好,不能修改。
索引分片推荐配置方案:
1.每个分片推荐大小10-30GB
2.分片数量推荐 = 节点数量 * 1~3倍
思考:比如有1000GB数据,应该有多少个分片?多少个节点
1.每个分片20GB 则可以分为40个分片
2.分片数量推荐 = 节点数量 * 1~3倍 --> 40/2=20 即20个节点
ElasticSearch高级-脚本和JavaAPI的bulk批量操作、查询操作(match、term、模糊、布尔、聚合、高亮...)、ES集群搭建与管理_es bulk java-CSDN博客
【二】springboot整合swagger(自定义)(超详细)
【四】springboot整合mybatis-plus(超详细)(上)
【五】springboot整合mybatis-plus(超详细)(下)
【十】springboot整合redis实现启动服务即将热点数据保存在全局以及redis(超详细)
【十一】springboot整合quartz实现定时任务优化(超详细)
【十二】springboot整合线程池解决高并发(超详细,保你理解)
【十三】springboot整合异步调用并获取返回值(超详细)
【十四】springboot整合WebService(超详细)
【十五】springboot整合WebService(关于传参数)(超详细)
【十六】springboot整合WebSocket(超详细)
【十七】springboot整合WebSocket实现聊天室(超详细)
【十九】springboot整合ElasticSearch实战(万字篇)
【二十二】springboot整合activiti7(1) 实战演示篇
【二十三】springboot整合spring事务详解以及实战
【二十四】springboot使用EasyExcel和线程池实现多线程导入Excel数据
【二十五】springboot整合jedis和redisson布隆过滤器处理缓存穿透
【二十六】springboot实现多线程事务处理_springboot多线程事务
【二十七】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能