幾種常用方式:
1.定時更新別名指向,更新后原索引可刪除或保留
POST /_aliases { "actions": [ { "add": { "alias": "logs_current", "index": "logs_2018-10" } }, { "remove": { "alias": "logs_current", "index": "logs_2018-09" } }, { "add": { "alias": "last_3_months", "index": "logs_2018-10" } }, { "remove": { "alias": "last_3_months", "index": "logs_2018-07" } } ] }
2.更新索引指向並刪除原索引
PUT test PUT test_2 POST /_aliases { "actions" : [ { "add": { "index": "test_2", "alias": "test" } }, { "remove_index": { "index": "test" } } ] }
3.創建過濾視圖
創建索引
PUT /test1 { "mappings": { "_doc": { "properties": { "user" : { "type": "keyword" } } } } }
創建視圖 POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias2", "filter" : { "term" : { "user" : "kimchy" } } } } ] }
下面是索引相關操作基礎知識:
_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" } } ] }
通過別名指定routing
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias1", "routing" : "1" } } ] }
可以具體指定查詢和索引的routing
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias2", "search_routing" : "1,2", "index_routing" : "2" } } ] }
如果查詢使用別名,並且參數中也指定了routing,則routing使用二者的交集,使用2.
GET /alias2/_search?q=user:kimchy&routing=2,3
當索引別名指向多個索引時,進行寫操作,其中的一個索引必須被指定為寫索引,並且只能指定一個,否則則無法寫入。
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias1", "is_write_index" : true } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }
切換寫索引屬性,原子操作
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias1", "is_write_index" : false } }, { "add" : { "index" : "test2", "alias" : "alias1", "is_write_index" : true } } ] }
_alias:單個別名操作
創建別名
PUT /{index}/_alias/{name}
參數:
index:The index the alias refers to. Can be any of * | _all | glob pattern | name1, name2, …
name:The name of the alias. This is a required option.
routing:An optional routing that can be associated with an alias.
filter:An optional filter that can be associated with an alias.
PUT /logs_201305/_alias/2013
創建一個過濾視圖
PUT /users { "mappings" : { "_doc" : { "properties" : { "user_id" : {"type" : "integer"} } } } }
PUT /users/_alias/user_12 { "routing" : "12", "filter" : { "term" : { "user_id" : 12 } } }
創建索引時同時指定別名
PUT /logs_20162801 { "mappings" : { "_doc" : { "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
查詢別名
GET /logs_20162801/_alias/* GET /_alias/2016 GET /_alias/20* HEAD /_alias/2016 HEAD /_alias/20* HEAD /logs_20162801/_alias/*
參考資料: