Elasticsearch实战:常见错误及详细解决方案

news/2024/10/25 17:22:46/

Elasticsearch实战:常见错误及详细解决方案

1.read_only_allow_delete":“true”

当我们在向某个索引添加一条数据的时候,可能(极少情况)会碰到下面的报错:

{"error": {"root_cause": [{"type": "cluster_block_exception","reason": "blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"}],"type": "cluster_block_exception","reason": "blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"},"status": 403
}

上述报错是说索引现在的状态是只读模式(read-only),如果查看该索引此时的状态:

GET z1/_settings
#结果如下
{"z1" : {"settings" : {"index" : {"number_of_shards" : "5","blocks" : {"read_only_allow_delete" : "true"},"provided_name" : "z1","creation_date" : "1556204559161","number_of_replicas" : "1","uuid" : "3PEevS9xSm-r3tw54p0o9w","version" : {"created" : "6050499"}}}}
}

可以看到"read_only_allow_delete" : "true",说明此时无法插入数据,当然,我们也可以模拟出来这个错误:

PUT z1
{"mappings": {"doc": {"properties": {"title": {"type":"text"}}}},"settings": {"index.blocks.read_only_allow_delete": true}
}PUT z1/doc/1
{"title": "es真难学"
}

现在我们如果执行插入数据,就会报开始的错误。那么怎么解决呢?

  • 清理磁盘,使占用率低于 85%。
  • 手动调整该项,具体参考官网

这里介绍一种,我们将该字段重新设置为:

PUT z1/_settings
{"index.blocks.read_only_allow_delete": null
}

现在再查看该索引就正常了,也可以正常的插入数据和查询了。

2. illegal_argument_exception

有时候,在聚合中,我们会发现如下报错:

{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [age] 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": "z2","node": "NRwiP9PLRFCTJA7w3H9eqA","reason": {"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [age] 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 [age] 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 [age] 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
}

这是怎么回事呢?是因为,聚合查询时,指定字段不能是text类型。比如下列示例:

PUT z2/doc/1
{"age":"18"
}
PUT z2/doc/2
{"age":20
}GET z2/doc/_search
{"query": {"match_all": {}},"aggs": {"my_sum": {"sum": {"field": "age"}}}
}

当我们向elasticsearch中,添加一条数据时(此时,如果索引存在则直接新增或者更新文档,不存在则先创建索引),首先检查该age字段的映射类型。如上示例中,我们添加第一篇文档时(z1索引不存在),elasticsearch会自动的创建索引,然后为age字段创建映射关系(es 就猜此时age字段的值是什么类型,如果发现是text类型,那么存储该字段的映射类型就是text),此时age字段的值是text类型,所以,第二条插入数据,age的值也是text类型,而不是我们看到的long类型。我们可以查看一下该索引的mappings信息:

GET z2/_mapping
#mapping信息如下
{"z2" : {"mappings" : {"doc" : {"properties" : {"age" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}}}
}

上述返回结果发现,age类型是text。而该类型又不支持聚合,所以,就会报错了。解决办法就是:

  • 如果选择动态创建一篇文档,映射关系取决于你添加的第一条文档的各字段都对应什么类型。而不是我们看到的那样,第一次是text,第二次不加引号,就是long类型了不是这样的。
  • 如果嫌弃上面的解决办法麻烦,那就选择手动创建映射关系。首先指定好各字段对应什么类型。后续才不至于出错。

3.Result window is too large

很多时候,我们在查询文档时,一次查询结果很可能会有很多,而 elasticsearch 一次返回多少条结果,由size参数决定:

GET e2/doc/_search
{"size": 100000,"query": {"match_all": {}}
}

而默认是最多范围一万条,那么当我们的请求超过一万条时(比如有十万条),就会报:

Result window is too large, from + size must be less than or equal to: [10000] but was [100000]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.

意思是一次请求返回的结果太大,可以另行参考 scroll API或者设置index.max_result_window参数手动调整size的最大默认值:

#kibana中设置
PUT e2/_settings
{"index": {"max_result_window": "100000"}
}
#Python中设置
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.put_settings(index='e2', body={"index": {"max_result_window": 100000}})

如上例,我们手动调整索引e2size参数最大默认值到十万,这时,一次查询结果只要不超过 10 万就都会一次返回。 注意,这个设置对于索引essize参数是永久生效的。

4.持续更新中

更多优质内容请关注公号:汀丶人工智能;会提供一些相关的资源和优质文章,免费获取阅读。


http://www.ppmy.cn/news/1199996.html

相关文章

R语言环境下使用curl库做的爬虫代码示例

curl库是一个用于传输数据的工具和库,它支持多种协议,包括HTTP、FTP、SMTP等。在爬虫中,curl库可以用来获取网页内容,从而实现爬取网页的功能。通过设置curl的选项,可以实现对网页的请求、响应、重定向等操作。在使用c…

陶瓷生产工艺数字孪生可视化管理平台,推动陶瓷工厂数字化转型新实践

聚焦国家战略需求和先进制造业发展方向,加快数字化发展战略部署,全面推进制造业数字化转型,推进智能制造典型场景和示范工厂的标杆引领,助力陶瓷工业的数字化、网络化、智能化并行推进、融合发展。数字孪生、物联网、云计算、人工…

关于Andorid Studio中build.gradle和setting.gradle配置变更

Build was configured to prefer settings repositories over project repositories but repository Google was added by build file xx/build.gradle 报错的原因是因为,在Android Gradle 7.x 版本中 Android Studio倾向于将下属依赖添加到setting.gradle中&#x…

RK3566上运行yolov5模型进行图像识别

一、简介 本文记录了依靠RK官网的文档,一步步搭建环境到最终在rk3566上把yolov5 模型跑起来。最终实现的效果如下: 在rk3566 板端运行如下app: ./rknn_yolov5_demo model/RK356X/yolov5s-640-640.rknn model/bus.jpg其中yolov5s-640-640.r…

[黑马程序员Pandas教程]——Pandas数据结构

目录: 学习目标认识Pandas中的数据结构和数据类型Series对象通过numpy.ndarray数组来创建通过list列表来创建使用字典或元组创建s对象在notebook中不写printSeries对象常用API布尔值列表获取Series对象中部分数据Series对象的运算DataFrame对象创建df对象DataFrame…

网络运维Day01

文章目录 环境准备OSI七层参考模型什么是协议?协议数据单元(PDU)设备与层的对应关系什么是IP地址?IP地址分类IP的网络位和主机位IP地址默认网络位与主机位子网掩码默认子网掩码查看IP地址安装CISCO汉化CISCO(可选操作) CISCO之PC机器验证通信 CISCSO之交…

Golang面试题从浅入深高频必刷「2023版」

大家好,我是阳哥。专注Go语言的学习经验分享和就业辅导。 Go语言特点 Go语言相比C/Java等语言是优雅且简洁的,是我最喜爱的编程语言之一,它既保留了C的高性能,又可以像Java,Python优雅的调用三方库和管理项目&#x…

某某盾-滑块验证-自动获取validate值-(逆向js+python)

我是标题 1.从get?网站获取滑块图片以及token1.1获取fp值1.2 获取cb值1.3 模拟发包 2.获取滑块移动距离3.发包获取最终的validate值3.1轨迹生成3.2 check网站发包3.3 获取data值 4.结论 本实验是根据某某盾示例网站 主要分为两个部分 1.从get?网站获取滑…