简介
索引别名就像一个快捷方式或软连接,可以指向一个或多个索引,也可以给任何一个需要索引名的API来使用,而且别名不能与索引同名。
别名带给我们极大的灵活性,允许我们做下面这些:
- 在运行的集群中可以无缝的从一个索引切换到另一个索引。
 - 给多个索引分组。
 - 给索引的一个子集创建视图
索引和别名并非是一对一的关系,一个索引可以对应多个别名,同样一个别名可以对应多个索引
 
别名还可以与筛选器关联,筛选器将在搜索和路由值时自动应用,别名不能与索引同名。
Elasticsearch 中有两种方式管理别名: _alias 用于单个操作, _aliases 用于执行多个原子级操作。
查询别名
直接调用_aliasAPI的GET方法可以看到索引的别名,下面的例子中就是一个没有创建别名的索引。
查询指定索引的别名
GET localhost:9200/employee/_alias
查询所有索引的别名
GET localhost:9200/_alias
{"test_city_info3": {"aliases": {}},"employee": {"aliases": {}},"city_info": {"aliases": {}}}
单个索引别名
# 创建POST /_aliases{"actions" : [{ "add" : { "index" : "test1", "alias" : "alias1" } }]}# 删除POST /_aliases{"actions" : [{ "remove" : { "index" : "test1", "alias" : "alias1" } }]}# 重命名# 重命名别名是一个简单的删除然后指向新的索引。这个操作是原子性的,因此不需要担心短时间内的别名不指向一个索引POST /_aliases{"actions" : [{ "remove" : { "index" : "test1", "alias" : "alias1" } },{ "add" : { "index" : "test2", "alias" : "alias1" } }]}
将别名与多个索引关联
POST /_aliases{"actions" : [{ "add" : { "index" : "test1", "alias" : "alias1" } },{ "add" : { "index" : "test2", "alias" : "alias1" } }]}
亦可以通过索引数组的方式来实现
POST /_aliases{"actions" : [{ "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }]}
对于上面的示例,还可以将别名关联到拥有公共名称的多个索引:
POST /_aliases{"actions" : [{ "add" : { "index" : "test*", "alias" : "all_test_indices" } }]}
Filtered Aliases
过滤器别名提供了一个简单的方法对同一个索引来创建不同的“视图”。过滤器能够使用Query DSL来定义并且被应用到所有的搜索,统计,通过查询删除和其它类似的行为。
为了创建一个带过滤器的别名,首先需要确保映射的字段已经存在于mapping中。
PUT /test1{"mappings": {"_doc": {"properties": {"user" : {"type": "keyword"}}}}}
然后我们可以创建一个在user字段上带过滤器的别名。
POST /_aliases{"actions" : [{"add" : {"index" : "test1","alias" : "alias2","filter" : { "term" : { "user" : "kimchy" } }}}]}
这样设置之后,我们通过 test1 这个 index 直接进行搜索可以看到索引的全部文档,但是通过 alias2 这个别名就只能看到符合过滤器过滤后的结果了,即只有一个 user 为 “kimchy” 的结果。
Routing
可以将路由值与别名关联。这个特性可以与过滤别名一起使用,以避免不必要的碎片操作。
何为路由?
所有的文档 API( get 、 index 、 delete 、 bulk 、 update 以及 mget )都接受一个叫做 routing 的路由参数 ,通过这个参数我们可以自定义文档到分片的映射。一个自定义的路由参数可以用来确保所有相关的文档—-例如所有属于同一个用户的文档都被存储到同一个分片中。
以下命令创建一个指向索引 test 的新别名 alias1。创建 alias1 后,所有具有此别名的操作将自动修改为使用值 1 进行路由:
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "routing" : "1"
            }
        }
    ]
}
还可以为搜索和索引操作指定不同的路由值
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias2",
                 "search_routing" : "1,2",
                 "index_routing" : "2"
            }
        }
    ]
}
如上例所示,搜索路由(search_routing)可能包含几个用逗号分隔的多个值,但是 索引路由(index_routing)就只能包含一个值。
如果使用路由别名的搜索操作也有路由参数,则使用搜索别名路由和参数中指定的路由的交集。例如,下面的命令将使用“2”作为路由值。因为搜索操作中有路由参数2,3,而搜索路由设置的是1,2,所以取交集即为2。
GET /alias2/_search?q=user:kimchy&routing=2,3
Write Index
如果一个别名只映射了一个真实索引,则可以使用别名进行index api(即索引文档,写文档),但如果一个别名同一时间映射了多个索引,默认是不能直接使用别名进行索引文档,因为ES不知道文档该发往哪个索引。
当一个别名关联多个索引的时候,此时需要为索引设置参数is_write_index,被设置为true的索引,当对别名写数据的时候数据会被写到此索引中。
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "is_write_index" : true
            }
        },
        {
            "add" : {
                 "index" : "test2",
                 "alias" : "alias1"
            }
        }
    ]
}
例子
以上面学生的例子, student_index 是一个指向当前真实索引的别名。真实索引包含一个版本号: student_index_v1 , student_index_v2 等等。
首先,创建索引 student_index_v1 ,然后将别名 student_index 指向它:
PUT /student_index_v1  // 创建索引 student_index_v1 。
PUT /student_index_v1/_alias/student_index  //设置别名 student_index 指向 student_index_v1 。
你可以检测这个别名指向哪一个索引:
GET /*/_alias/student_index
或哪些别名指向这个索引:
GET /student_index_v1/_alias/*
两者都会返回下面的结果:
{
    "student_index_v1" : {
        "aliases" : {
            "student_index" : { }
        }
    }
}
然后,我们决定修改索引中一个字段的映射。当然,我们不能修改现存的映射,所以我们必须重新索引数据。 首先, 我们用新映射创建索引 student_index_v2 :
PUT /student_index_v2
{
    "mappings": {
        "my_type": {
            "properties": {
                "tags": {
                    "type":   "string",
                    "index":  "not_analyzed"
                }
            }
        }
    }
}
然后我们将数据从 student_index_v1 索引到 student_index_v2 ,下面的过程在重新索引你的数据 中已经描述过。一旦我们确定文档已经被正确地重索引了,我们就将别名指向新的索引。
一个别名可以指向多个索引,所以我们在添加别名到新索引的同时必须从旧的索引中删除它。这个操作需要原子化,这意味着我们需要使用 _aliases 操作:
POST /_aliases
{
    "actions": [
        { "remove": { "index": "student_index_v1", "alias": "student_index" }},
        { "add":    { "index": "student_index_v2", "alias": "student_index" }}
    ]
}
                    