您好,目前我需要将只读索引segment合并,有几个问题想要求教
POST /my_index/_forcemerge?max_num_segments=1
会不会吃光所有的机器资源,造成服务暂时不可用(optimize?max_num_segments=1就会吃光所有资源),但是我没有从官方文档找到_forcemerger 这种方式的资源消耗。
(目前我全部使用的默认值)
死磕 Elasticsearch 知识星球 http://t.cn/RmwM3N9
这个涉及到基础概念,为保证说法的准确性,如下会贴上官方链接和其他来源地址,以便更深入理解好段合并的相关知识。

图片来源:https://medium.com/@hansrajchoudhary_88463/
如图所示,自顶向下看,
在查询的时,会把所有的segment查询结果汇总归并为最终的分片查询结果返回。
在 lucene 中,为了实现高索引速度,故使用了segment 分段架构存储。
一批写入数据保存在一个段中,其中每个段是磁盘中的单个文件。
由于两次写入之间的文件操作非常繁重,因此将一个段设为不可变的,以便所有后续写入都转到New段。
由于自动刷新流程每秒会创建一个新的段(由动态配置参数:refresh_interval 决定),这样会导致短时间内的段数量暴增。
而段数目太多会带来较大的麻烦。
Elasticsearch 通过在后台进行段合并来解决这个问题。
小的段被合并到大的段,然后这些大的段再被合并到更大的段。
段合并的时候会将那些旧的已删除文档从文件系统中清除。
被删除的文档(或被更新文档的旧版本)不会被拷贝到新的大段中。
启动段合并不需要你做任何事。进行索引和搜索时会自动进行。
可以物理上删除document,但只是做了删除标记,物理上并没有删除。
原因:段合并会移除被标记为已删除的那些文档。
早期版本的文档有说明如下:
optimize API(现在已废弃,原理一致)
optimize API大可看做是 强制合并 API。它会将一个分片强制合并到 max_num_segments 参数指定大小的段数目。
这样做的意图是减少段的数量(通常减少到一个),来提升搜索性能。
资源消耗的官方解读
orce merge should only be called against an index after you have finished writing to it. Force merge can cause very large (>5GB) segments to be produced, and if you continue to write to such an index then the automatic merge policy will never consider these segments for future merges until they mostly consist of deleted documents. This can cause very large segments to remain in the index which can result in increased disk usage and worse search performance.
一句话:导致磁盘io消耗和影响检索性能。
Force merge API
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html
以下是老版本文档解读,原理一致可参考,api已过时。
段合并
https://www.elastic.co/guide/cn/elasticsearch/guide/current/merge-process.html
请注意,使用 optimize API 触发段合并的操作不会受到任何资源上的限制。
这可能会消耗掉你节点上全部的 I/O 资源, 使其没有“富裕”资源来处理搜索请求,从而有可能使集群失去响应。
如果你想要对索引执行 optimize,你需要先使用分片分配(查看 迁移旧索引)把索引移到一个安全的节点,再执行。
是的,非常耗费资源,建议在非业务密集实践操作。
我的线上环境,我都是凌晨1点段合并(脚本控制,晚上没人操作系统)
推荐:
https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-merge.html
索引性能技巧
https://www.elastic.co/guide/cn/elasticsearch/guide/current/indexing-performance.html#segments-and-merging
1、https://medium.com/@hansrajchoudhary_88463/elasticsearch-things-you-should-know-about-es-to-be-useful-fbc537a04086
2、https://stackoverflow.com/questions/30151566/why-are-documents-in-elasticsearch-immutable
3、https://www.elastic.co/cn/blog/found-elasticsearch-from-the-bottom-up
本文分享自 铭毅天下Elasticsearch 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!