索引別名API允許使用一個名字來作為一個索引的別名,所有API會自動將別名轉換為實際的索引名稱。 別名也可以映射到多個索引,別名不能與索引具有相同的名稱。別名可以用來做索引遷移和多個索引的查詢統一,還可以用來實現視圖的功能
查看所有別名
GET /_aliases
查看某個別名下的索引
GET /_alias/alias_name
查看別名以2017開頭的下的索引
GET /_alias/2017
查看某個索引的別名
GET /index_name/_alias
添加並刪除別名
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" } }
]
}
還可以使用別名實現類似視圖的功能
PUT /test1
{
"mappings": {
"type1": {
"properties": {
"user" : {
"type": "keyword"
}
}
}
}
}
創建一個user等於kimchy的視圖
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test1",
"alias" : "alias2",
"filter" : { "term" : { "user" : "kimchy" } }
}
}
]
}
添加單個別名
PUT /{index}/_alias/{name}
examples:
PUT /logs_201305/_alias/2013
PUT /users/_alias/user_12
{
"routing" : "12",
"filter" : {
"term" : {
"user_id" : 12
}
}
}
創建索引時也可以指定別名
PUT /logs_20162801
{
"mappings" : {
"type" : {
"properties" : {
"year" : {"type" : "integer"}
}
}
},
"aliases" : {
"current_day" : {},
"2016" : {
"filter" : {
"term" : {"year" : 2016 }
}
}
}
}
刪除別名
DELETE /{index}/_alias/{name}
index * | _all | glob pattern | name1, name2, …
name * | _all | glob pattern | name1, name2, …
DELETE /logs_20162801/_alias/current_day