Elasticsearch alias

Elasticsearch Alias


1.操作别名的两个方法:
_alias:单个操作
_aliases:多个操作,原子性的操作

2.创建别名

1
2
3
4
5
6
7
curl -XPUT 'localhost:9200/dm_v1/_alias/dm'
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "add" : { "index" : "dm_v1", "alias" : "dm_alias" } }
]
}'

3.删除别名

1
2
3
4
5
6
7
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "dm_v1", "alias" : "dm_alias" } }
]
}'
curl -XDELETE 'localhost:9200/dm_v1/_alias/dm_alias'

4.删除别名的同时添加别名到新的索引,该操作时原子性的,不用担心存在别名没有指向任何索引的瞬间

Renaming an alias is a simple remove then add operation within the same API. This operation is atomic, no need to worry about a short period of time where the alias does not point to an index:

1
2
3
4
5
6
7
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "dm_v1", "alias" : "dm" } },
{ "add" : { "index" : "dm_v2", "alias" : "dm" } }
]
}'

5.查询别名
通过别名查询所指向的索引:

1
2
curl -XGET 'localhost:9200/_alias/dm'
curl -XGET 'localhost:9200/_alias/dm*'

查询指向该索引下的所有别名:

1
curl -XGET 'localhost:9200/dm_v2/_alias/*'

6.也可以通过head检测别名是否存在

1
2
3
curl -XHEAD -i 'localhost:9200/_alias/dm'
curl -XHEAD -i 'localhost:9200/_alias/dm*'
curl -XHEAD -i 'localhost:9200/dm_v2/_alias/*'