ElasticSearch之Force merge API

使用本方法,可以触发强制合并操作。

默认情况下,ElasticSearch会在后台周期性触发合并操作,因此不需要用户刻意使用本方法。

使用强制合并的弊端:

  • 可能会产生大于5G的segment对象,而ElasticSearch后台自动触发的合并操作会跳过此类大型segment对象。
  • 假如定期执行强制合并,将导致快照的成本增高,原因是强制合并产生的segment的变动,导致无法增量创建快照。

方法参数
max_num_segments

合并操作完成后,创建的segment对象的数量。

当指定为1时,表示完全合并。

未指定本参数时,表示仅当符合条件时,才会执行合并操作。

only_expunge_deletes,默认值为false

  • true,检查全部segment对象,对于标记为删除记录的占比超出参数index.merge.policy.expunge_deletes_allowedsegment,执行合并操作。
  • false,不检查删除记录的占比。

wait_for_completion,默认值为true

  • true,表示同步调用,需要等待本次强制合并任务结束,调用才会返回。
  • false,表示异步调用,可以使用返回的任务标识来查询进度,但注意触发的任务不会被取消。假如有多个任务被触发,则会被调度器放在队列中,逐个执行。

注意only_expunge_deletesmax_num_segments不能同时使用。

相关参数
index.merge.policy.expunge_deletes_allowed
index中已被标记为删除状态的记录的比例,百分数,默认值为10

当前only_expunge_deletes取值为true时使用。

使用样例

创建测试用的index,命名为testindex_001,命令样例,如下:

shell 复制代码
curl -X PUT "https://localhost:9200/testindex_001?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

shell 复制代码
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "testindex_001"
}

执行时指定index的名称testindex_001,命令样例,如下:

shell 复制代码
curl -X POST "https://localhost:9200/testindex_001/_forcemerge?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

shell 复制代码
{
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

执行时不指定index的名称,命令样例,如下:

shell 复制代码
curl -X POST "https://localhost:9200/_forcemerge?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

shell 复制代码
{
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

执行时不指定index的名称,异步调用,命令样例,如下:

shell 复制代码
curl -X POST "https://localhost:9200/_forcemerge?wait_for_completion=false&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

shell 复制代码
{
  "task" : "aKgBu7LgS9a6iPYH8n2JPw:103209"
}

相关资料

相关推荐
Elastic 中国社区官方博客16 小时前
Elasticsearch:使用 Agent Builder 的 A2A 实现 - 开发者的圣诞颂歌
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
浅时光_c17 小时前
3 shell脚本编程
linux·开发语言·bash
历程里程碑17 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
Lucis__18 小时前
一文读懂TCP通信机制:基于相关API构建可靠性连接
linux·网络·tcp/ip
_深海凉_18 小时前
LeetCode热题100-有效的括号
linux·算法·leetcode
零号全栈寒江独钓20 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
左手厨刀右手茼蒿20 小时前
Linux 内核中的进程管理:从创建到终止
linux·嵌入式·系统内核
geinvse_seg20 小时前
中小团队如何低成本搭建项目管理系统?基于 Ubuntu 的 Dootask 私有化部署实战
linux·运维·ubuntu
CSCN新手听安20 小时前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆
丶伯爵式20 小时前
Ubuntu 24.04 更换国内软件源指南 | 2026年3月26日
linux·运维·ubuntu·国内源·升级