主要针对1.5版本,主要参考自官网资料,可以理解为一个翻译+实践+扩充的版本
在endpoint中操作
增加别名
能够通过如下的方式添加
PUT /{index}/_alias/{name}
- index为别名指向的index,可以是 * | _all | glob pattern | name1, name2, …
- name为别名的名字,这项必须
- routing 可选,别名绑定的路由
- filter 可选,别名绑定的过滤器
也可以使用复数个_aliases
例子:
curl -XPUT 'localhost:9200/logs_201305/_alias/2013'
有路由和过滤器的例子
首先创建一个有user_id字段的index
curl -XPUT 'localhost:9200/users' -d '{
"mappings" : {
"user" : {
"properties" : {
"user_id" : {"type" : "integer"}
}
}
}
}'
然后添加带路由和过滤器的alias
curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
"routing" : "12",
"filter" : {
"term" : {
"user_id" : 12
}
}
}'
当然,路由也可以拆开为index_routing和search_routing
curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
"index_routing" : "12",
"search_routing" : "11",
"filter" : {
"term" : {
"user_id" : 12
}
}
}'
在index创建期间创建别名
可以在创建index期间创建别名
curl -XPUT localhost:9200/logs_20142801 -d '{
"mappings" : {
"type" : {
"properties" : {
"year" : {"type" : "integer"}
}
}
},
"aliases" : {
"current_day" : {},
"2014" : {
"filter" : {
"term" : {"year" : 2014 }
},
"routing": "1"
}
}
}'
可以观测到创建了current_day,2014两个别名
~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search
current_day logs_20142801 - - -
2014 logs_20142801 * 1 1
删除别名
形式如 /{index}/_alias/{name}
- index * | _all | glob pattern | name1, name2
- name * | _all | glob pattern | name1, name2
也可以使用附属各_aliases。例子如下
curl -XDELETE 'localhost:9200/users/_alias/user_12'
使用_cat查看alias
用 _cat查看alias
curl 'localhost:9200/_cat/aliases?v'
成功则返回别名相关信息
alias index filter routing.index routing.search
alias11 test - - -
alias11 test1 - - -
暴力删除别名和指向的index,慎用!
直接用-XDELETE可以删除别名,例如上述例子中alias11指向test和test1
~ % curl -XDELETE 'localhost:9200/alias*'
{"acknowledged":true}%
~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search
用-XDELETE就直接删除掉了,不过test和test1也就不见了,所以要慎用!
同理,删除了某个index,也会把这个index和别名的关系也删除了,如果该别名只有指向该index,那么该别名就会被删除!,具体示例如下:
~ % curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias11"
}
}
]
}'
{"acknowledged":true}% ~ % curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test1",
"alias" : "alias11"
}
}
]
}'
{"acknowledged":true}% ~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search
alias11 test * 1 1
alias11 test1 * 1 1
~ % curl -XDELETE 'localhost:9200/test'
{"acknowledged":true}% ~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search
alias11 test1 * 1 1
~ % curl -XDELETE 'localhost:9200/test1'
{"acknowledged":true}% ~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search
检索存在的别名
获取index与别名的API允许按照别名的名字和索引的名字进行过滤。这个api重定向到主服务器并且获取请求的index别名,如果可能的话,这个api仅仅序列化找到的index的别名。
形式为
/{index}/_alias/{alias}
- index 要获取别名的index名字。能够通过通配符支持部分名称写法,可以用逗号隔开使用多个index名字,也可以使用指向index的别名
- alias 在返回的向应用的alias的名字,和index的选项相似,支持通配符和用逗号隔开多个别名
- ignore_unavailable 如果一个指定名字的index不存在怎么办,如果true的话这些index将会被忽视。
警告:在未来版本的ElasticSearch中,默认的multiple indices选择可能会报错如果其中一个index不存在,这么做是为了和其它的index get API保持一致。
例子
获取index logs_20142801的所有别名
curl -XGET 'localhost:9200/logs_20142801/_alias/*?pretty'
返回的值为
{
"logs_20142801" : {
"aliases" : {
"current_day" : { },
"2014" : {
"filter" : {
"term" : { "year" : 2014 } },
"index_routing" : "1",
"search_routing" : "1"
}
}
}
}
所有和别名alias2相关的index
curl -XGET 'localhost:9200/_alias/alias2?pretty'
返回值为
{
"test" : {
"aliases" : {
"alias2" : {
"filter" : {
"term" : { "user" : "kitty" } },
"index_routing" : "1",
"search_routing" : "2,3"
}
}
},
"test1" : {
"aliases" : {
"alias2" : {
"filter" : {
"term" : { "year" : 2014 } },
"index_routing" : "1",
"search_routing" : "1"
}
}
}
}
所有以alias开后的别名相关的index
curl -XGET 'localhost:9200/_alias/alias*?pretty'
返回的值为
{
"test" : {
"aliases" : {
"alias2" : {
"filter" : {
"term" : { "user" : "kitty" } },
"index_routing" : "1",
"search_routing" : "2,3"
},
"alias3" : {
"filter" : {
"term" : { "user" : "hello" } },
"index_routing" : "2",
"search_routing" : "1"
}
}
},
"test1" : {
"aliases" : {
"alias2" : {
"filter" : {
"term" : { "year" : 2014 } },
"index_routing" : "1",
"search_routing" : "1"
},
"alias4" : { }
}
}
}
还有一个HEAD变量的获取index与别名的api来检查一个index的别名是否存在,这个index别名是否存在的api也支持和获取index与别名api相似。例如
curl -XHEAD -i 'localhost:9200/_alias/alias2'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
curl -XHEAD -i 'localhost:9200/_alias/alias*'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
curl -XHEAD -i 'localhost:9200/test/_alias/*'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
curl -XHEAD -i 'localhost:9200/not-exist/_alias/*'
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0