【ElasticSearch】索引生命周期管理(三) 避坑指南
背景
主要是针对在使用索引生命周期的去管理索引的过程中,记录所踩到坑,避免同样的问题再次发生
问题
1. 索引生命周期中设置各个阶段的市场以及索引rollover的时间不合理,导致整个索引数据被删除
例如需求如下:
业务需求:
数据至少保存半年,删除半年前的数据
生命周期设计:
hot阶段--->delete阶段 即 180天后,当前索引文件的生命周期由hot阶段变为delete阶段,变为delete阶段的文件将被删除
生命周期实现(错误)示范
PUT _ilm/policy/test_ilm_policy
{
"policy" : {
"phases" : {
"hot" : {
"min_age" : "0ms",
"actions" : {
"rollover" : {
"max_age" : "30d"
},
"set_priority" : {
"priority" : 100
}
}
},
"delete" : {
"min_age" : "180d",
"actions" : {
"delete" : { }
}
}
}
}
}
生命周期错误原因:
rollerover粒度过大,导致索引进入delete阶段会删除整个30天的索引的数据
分析如下:
当处于hot阶段的例如:索引A-0001经历了30天后,进行了rollover,意味着旧索引在过150天后,索引A-0001即将进入delete阶段,但是在索引A-0001中包含有第1天-第30天的数据,理论上只要删除第一天的数据即可,因此需要修改roller的时间粒度
修改后的生命周期配置如下:(rollover粒度修改为1天)
PUT _ilm/policy/test_ilm_policy_v2
{
"policy" : {
"phases" : {
"hot" : {
"min_age" : "0ms",
"actions" : {
"rollover" : {
"max_age" : "1d"
},
"set_priority" : {
"priority" : 100
}
}
},
"delete" : {
"min_age" : "180d",
"actions" : {
"delete" : { }
}
}
}
}
}
2. 初始索引设置失败,导致索引被应用到生命周期和索引模板时,出现异常 The write index may be explicitly disabled using is_write_index=false
索引模板如下:
PUT _template/test_template
{
"order" : 0,
"index_patterns" : [
"text-audit*"
],
"settings" : {
"index" : {
"lifecycle" : {
"name" : "text_audit_ilm_policy_3",
"rollover_alias" : "text-audit"
},
"number_of_shards" : "1",
"number_of_replicas" : "0"
}
},
"mappings" : {
"text_audit_type" : {
"_source" : {
"enabled" : true
},
"properties" : {
"bizType" : {
"type" : "keyword"
},
.....省略
}
}
}
},
"aliases" : {
}
}
}
索引生命周期如下:
PUT _ilm/policy/test_ilm_policy_v2
{
"policy" : {
"phases" : {
"hot" : {
"min_age" : "0ms",
"actions" : {
"rollover" : {
"max_age" : "1d"
},
"set_priority" : {
"priority" : 100
}
}
},
"delete" : {
"min_age" : "180d",
"actions" : {
"delete" : { }
}
}
}
}
}
设置初始索引(错误)
PUT text-audit-0001
{
"aliases": {
"text-audit": {
}
}
}
配置完成后整个逻辑应该
创建索引test-audit-0001,且设置别名为test-audit,由于指定了索引模板test_template,因此这个索引会套用模板去设置对应的字段和设置,以及生命周期配置
但是
当索引进行rollover时,新索引为test-audit-0002,此时 test-audit-0002和test-audit-0001都拥有相同的别名 test-audit ,项目中也是使用别名操作,
如果一个索引别名被应用到了多个索引,则必须制定有且一个索引的属性is_write_index=true,如果没制定该属性或指定多个相同别名的索引is_write_index=true ,则会出现异常
The write index may be explicitly disabled using is_write_index=false
正确的设置初始索引
PUT text-audit-0001
{
"aliases": {
"text-audit": {
"is_write_index":true
}
}
}
分析:
正确的设置初始索引test-audit-0001,test-audit-0001 进行rollerover为test-audit-0002时,索引将会应用到别名,并且将is_write_index=true传递给test-audit-0002,同时test-audit-0001索引中的is_write_index=true将会变为false
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2020-08-26 【Springboot】如何将tomcat替换为jboss
2020-08-26 【Springboot】Springboot自动装配原理