Elasticsearch Alias第一篇 Index Alias

来源:互联网 发布:zk 集群通讯端口 编辑:程序博客网 时间:2024/06/05 06:58

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

Index Alias

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

/_searchSearch all types in all indices/gb/_searchSearch all types in the gb index/gb,us/_searchSearch all types in the gb and us indices/g*,u*/_searchSearch all types in any indices beginning with g or beginning with u/gb/user/_searchSearch type user in the gb index/gb,us/user,tweet/_searchSearch types user and tweet in the gb and us indices/_all/user,tweet/_searchSearch 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中间的字段完全不同,也仍然会报错。

0 0
原创粉丝点击