Elasticsearch - 索引库别名使用详解
1,索引库别名使用案例
(1)在工作中使用 ES 收集应用的运行日志,每个星期创建一个索引库,这样时间长了就会创建很多的索引库,操作和管理的时候很不方便。
(2)由于新增索引数据只会操作当前这个星期的索引库,所以为了使用方便,我们就创建了两个索引库别名:curr_week 和 last_3_month。
- curr_week:这个别名指向当前这个星期的索引库,新增数据使用这个索引库别名。
- last_3_month:这个别名指向最近三个月的所有索引库,因为我们的需求是需要查询最近三个月的日志信息。
(3)后期只需要修改这两个别名和索引库之间的指向关系即可,应用层代码不需要任何改动。
2,索引库别名的建立与使用
(1)假设 ES 已经收集了一段时间的日志数据,每一星期都会创建一个索引库,所以目前创建了 4 个索引库:
curl -H "Content-Type: application/json" -XPUT 'http://192.168.121.128:9200/log_20260301/' curl -H "Content-Type: application/json" -XPUT 'http://192.168.121.128:9200/log_20260308/' curl -H "Content-Type: application/json" -XPUT 'http://192.168.121.128:9200/log_20260315/' curl -H "Content-Type: application/json" -XPUT 'http://192.168.121.128:9200/log_20260322/'
- 然后分别向每个索引库里面初始化 1 条测试数据:
curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/log_20260301/_doc/1' -d'{"log":"info->20260301"}' curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/log_20260308/_doc/1' -d'{"log":"info->20260308"}' curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/log_20260315/_doc/1' -d'{"log":"info->20260315"}' curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/log_20260322/_doc/1' -d'{"log":"info->20260322"}'
(2)为了使用方便,我们创建了两个索引库别名:curr_week 和 last_3_month。
- curr_week 指向最新的索引库:log_20260322
curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/_aliases' -d ' { "actions" : [ { "add" : { "index" : "log_20260322", "alias" : "curr_week" } } ] }'
- last_3_month 指向之前 3 个月内的索引库,我们可以同时增加多个索引别名。
curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/_aliases' -d ' { "actions" : [ { "add" : { "index" : "log_20260301", "alias" : "last_3_month" } }, { "add" : { "index" : "log_20260308", "alias" : "last_3_month" } }, { "add" : { "index" : "log_20260315", "alias" : "last_3_month" } } ] }'
(3)以后使用的时候,想要操作当前星期内的数据就使用 curr_week 这个索引库别名就行了。可以发现这里面就 1 条数据,和使用索引库 log_20260322 查询的结果是一样的。
curl -XGET 'http://192.168.121.128:9200/curr_week/_search?pretty'

- 再使用 last_3_month 查询一下数据。这里面返回了 log_20260301、log_20260308 和 log_20260315 这 3 个索引库里面的数据。
curl -XGET 'http://192.168.121.128:9200/last_3_month/_search?pretty'

3,索引库别名的修改
(1)过了一个星期之后,又多了一个新的索引库:log_20260329,我们创建这个索引库并初始化一条数据。
curl -H "Content-Type: application/json" -XPUT 'http://192.168.121.128:9200/log_20260329/' curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/log_20260329/_doc/1' -d'{"log":"info->20260329"}'
(2)此时就需要修改 curr_week 别名指向的索引库了,需要先删除之前的关联关系,再增加新的。
- 首先删除 curr_week 和 log_20260322 之间的关联关系。
curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/_aliases' -d ' { "actions" : [ { "remove" : { "index" : "log_20260322", "alias" : "curr_week" } } ] }'
- 然后新增 curr_week 和 log_20260329 之间的关联关系:
curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/_aliases' -d ' { "actions" : [ { "add" : { "index" : "log_20260329", "alias" : "curr_week" } } ] }'
- 此时再查询 curr_week 中的数据其实就是查询索引库 log_20260329 里面的数据了。
curl -XGET 'http://192.168.121.128:9200/curr_week/_search?pretty'

(3)同样地,我们需要再把索引库 log_20260322 添加到 last_3_month 别名中,并且移除原来的 log_20260301 索引库别名。
提示:这些关联别名映射关系和移除别名映射关系的操作需要写个脚本定时执行,这样就可以实现别名自动关联到指定索引库了。
curl -H "Content-Type: application/json" -XPOST 'http://192.168.121.128:9200/_aliases' -d ' { "actions" : [ { "remove" : { "index" : "log_20260301", "alias" : "last_3_month" } }, { "add" : { "index" : "log_20260322", "alias" : "last_3_month" } } ] }'
- 再使用 last_3_month 查询一下数据。这里面返回了 log_20260308、log_20260315 和 log_20260322 这 3 个索引库里面的数据。
curl -XGET 'http://192.168.121.128:9200/last_3_month/_search?pretty'

4,查看索引库别名
(1)假设时间长了,我们如果忘记了这个别名下对应的都有哪些索引库,可以使用下面的方法查看一下:
curl -XGET 'http://192.168.121.128:9200/_alias/curr_week?pretty' curl -XGET 'http://192.168.121.128:9200/_alias/last_3_month?pretty'

(2)如果想知道哪些别名指向了这个索引,可以这样查看:
curl -XGET 'http://192.168.121.128:9200/log_20260322/_alias/*?pretty'
