《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.11.Index alias(1) https://developer.aliyun.com/article/1230380
JSON 体内支持的参数包括必填参数和可选参数。
详情如下:
必填参数:
index
参数类型string,支持通配符。如果indices没有指定,则该参数必须指定。
注意:不能向索引别名添加数据流。
indices
参数类型string数组,该数组内的索引将被执行相应的动作。如果index没有指定,则该参数必须指定。
注意事项与index参数相同。
alias
参数类型string,为以逗号分隔或者通配符表示的索引,add,remove,delete别名。如果
aliases没有指定,则该参数必须指定。
aliases
参数类型string array,需要进行add,remove,delete的索引别名组。如果alias没有指定,则该参数必须指定。
可选参数:
filter
query object查询对象体,绑定了过滤查询的别名。如果指定,使用别名进行空查询,将只返回满足过滤条件的文档。
is_hidden
参数类型bool值,默认值为false;如果设置为true,使用通配符表达式别名进行搜索排除,该别名关联的数据将查询不到;除非在请求中使用expand_wildcards参数重写。对于共享同一个别名的所有索引,必须将此属性设置为相同的值。
must_exist
参数类型bool值,默认值为false,如果设置为ture,移除别名时,该别名必须存在。
is_write_index
参数类型bool值,默认值为false;如果设置为true,则可以直接使用该别名对关联的索引进行数据写入或者配置修改等操作;若别名绑定多个索引,则只能存在一个is_write_index值为
true的绑定。注意:在同一个索引is_hidden和is_write_index不能同时设置为true;当别名只绑定一个索引时无需现实设定该值为true,该别名具有写权限,但是当别名再次绑定另外一个索引,则别名的写权限取消,除非现实指定is_write_index的值为true。
routing
参数类型string,自定义值作为路由计算值,将操作路由到对应的分片上。
index_routing
参数类型string,自定义值作为路由计算值,将写入操作路由到对应的分片上。
search_routing
参数类型string,自定义值作为路由计算值,将查询操作路由到对应的分片上。
别名创建与修改示例
假设已经存在表user1和user2,为他们绑定别名,示例子如下:
#为 users1 表创建 index-alias-name1 别名 PUT /users1/_alias/index-alias-name1 #为 users1,users2 表创建 index-alias-name2 别名 PUT /users1,users2/_alias/index-alias-name2 #为 users 开头的索引创建 index-alias-name3别名 PUT /users*/_alias/index-alias-name3 #为 users1 索引添加具有路由和过滤功能的别名 routing-filter-index-alias PUT users1/_alias/routing-filter-index-alias { "routing" : "12", "filter" : { "term" : { "user_id" : 12 } } } #为 users1 索引添加路由别名routing-index-alias,路由计算值为12 PUT users1/_alias/routing-index-alias { "routing" : "12" } #为 users1 索引添加过滤别名filter-index-alias,过滤 user_id 为12 PUT users1/_alias/filter-index-alias { "filter" : { "term" : { "user_id" : 12 } } }
别名创建成功之后,如果别名与索引的关系,为一个别名只对应一个索引,或者有一个绑定关系的 is_write_index (后文会介绍)值为 true ;那么我们可以通过别名往索引写如数据。
#通过别名,写入数据 PUT index-alias-name1/_bulk?refresh=true {"index":{}} {"user_id":"tom123456-user1"} #通过路由别名,写入数据 PUT routing-index-alias/_bulk?refresh=true {"index":{}} {"user_id":"kimchy123456-routing"} #通过索引,写入数据 PUT users2/_bulk?refresh=true {"index":{}} {"user_id":"kimchy123456-user2"} {"index":{}} {"user_id":"12"}
插入数据后可以通过别名或者索引查询:
#以下三条查询语句的结果是等价的,返回2条数据 GET index-alias-name1/_search GET users1/_search GET routing-index-alias/_search #以下三条查询语句的结果是等价的(如果没有其他 users 开头的索引),返回四条数据 GET users1,users2/_search GET users*/_search GET index-alias-name2/_search #查询 user2 的索引,返回2条数据 GET users2/_search #返回值为空,因为索引 users1 插入的文档没有 user_id 值为2的 GET filter-index-alias/_search # 返回值为一条,因为索引 users2 有一条 user_id 值为2的文档 GET index-alias-name3/_search
批量创建索引别名示例
批量创建索引别名,即使用POST /_aliases中定义多个action。为test1 和test2索引绑定一个名称为alias1的别名。
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias1" } } { "add" : { "index" : "test2", "alias" : "alias1" } } ] }
重命名别名
如果我们需要对一个索引进行别名替换,只需要在同一个 API 中简单的先remove掉旧别名,然后绑定新的别名即可;该操作为原子型操作,无需担心别名在短时间内不指向索引
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test1", "alias" : "alias2" } } ] }
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.11.Index alias(3) https://developer.aliyun.com/article/1230377