有时候我们使用ES索引的别名更方便操作,比如不通索引设置相同别名时使用这个别名可以一起查询(注意只能查询,即读操作);别名可以随时增加、删除,方便某些时候的访问。
指定索引saas_product的别名为saas_product_batch_stock。还有更简便的方式:
PUT /{index}/_alias/{name}当然你也可以指定多个索引使用同一个别名:
POST /_aliases { "actions": [ { "add": { "index": "my_index_1", "alias": "my_index_alias" } }, { "add": { "index": "my_index_2", "alias": "my_index_alias" } } ] }或者这样写也行
POST /_aliases { "actions" : [ { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } } ] }甚至你也可以模糊匹配来添加索引别名:
POST /_aliases { "actions" : [ { "add" : { "index" : "test*", "alias" : "all_test_indices" } } ] }需要注意的是别名不能与索引名相同,否则无法添加。
更简便的方式:
DELETE /{index}/_alias/{alias}删除别名的同时也可以添加别名:
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }