Elasticsearch Alias第一篇 Index Alias

本文档详细介绍了Elasticsearch中Index Alias的使用,包括添加、删除、重命名和切换别名,以及如何创建指向多个index的别名。通过别名,可以方便地在多个index间进行操作,实现数据的快速切换和管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要针对1.5版本,主要参考自官网资料,可以理解为一个翻译+实践+扩充的版本

Index Alias

当针对某个指定的index进行操作时候,ElasticSearch中的API接受一个index的名字。当然在多个index的情况下也可以指定多个index。多个index使用可以如下:

/_search
Search all types in all indices
/gb/_search
Search all types in the gb index
/gb,us/_search
Search all types in the gb and us indices
/g*,u*/_search
Search all types in any indices beginning with g or beginning with u
/gb/user/_search
Search type user in the gb index
/gb,us/user,tweet/_search
Search types user and tweet in the gb and us indices
/_all/user,tweet/_search
Search types user and tweet in all indices

index aliases的API允许我们为一个index指定别名。一个别名能够被映射到多个index中,当指定了index对应已经有其它index对应的别名之后,别名可以自动扩展到多个index。一个别名能够关联上一个过滤器,当搜索和路由的时候能够自动使用这个过滤器。

add别名

这个一个关联别名alias1到index test1的例子

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}'

成功则返回

{"acknowledged":true}

remove别名

也可以删除别名

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}'

成功则返回

{"acknowledged":true}

重命名

重命名一个别名很简单,通过相同API的简单remove和add操作就可以了。这个操作是原子的,不用担心在很短的一段时间内别名并不指向任何一个index

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test1", "alias" : "alias2" } }
    ]
}'

成功则返回

{"acknowledged":true}

别名切换

当然,也可以通过同一个API实现alias到不同index的切换。在实际使用中,如果我们通过别名来访问ElasticSearch,那么可以通过别名指向index的切换来实现不同版本数据的快速切换。

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias2" } },
        { "add" : { "index" : "test", "alias" : "alias2" } }
    ]
}'

成功则返回

{"acknowledged":true}

指向多个index的别名

可以将一个别名指向多余一个index,通过简单的多个add操作

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}'

成功则返回

{"acknowledged":true}

如果一个别名指向多于一个index,那么通过其进行索引是错误的。例如alias4指向多于1个index,alias5只指向一个index,那么通过别名输入文档,结果如下:

~ % curl -XPOST 'localhost:9200/alias5/test1' -d '{"field1":"hello"}'
{"_index":"test1","_type":"test1","_id":"AVhntniVu0MIai42Pwvl","_version":1,"created":true}%   
~ % curl -XPOST 'localhost:9200/alias4/test1' -d '{"field1":"hello"}'                                                                                                                                                                       
{"error":"ElasticsearchIllegalArgumentException[Alias [alias4] has more than one indices associated with it [[test, test1]], can't execute a single index op]","status":400}%

通过只指向一个index的别名进行文档输入能够完成,但是通过指向多个index的索引进行文档输入会出现错误。即使别名指向的index中间的字段完全不同,也仍然会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值