本文共 6086 字,大约阅读时间需要 20 分钟。
ES中的索引别名是一种简单且非常实用的功能,当你为一个索引设置别名后,就可以通过这个别名来操作相关的API,ES会自动将别名映射到实际的索引名中,而这样做的好处是,为你在之后的扩展方面带来的极大的灵活性。
1、替换原索引:很显然,如果你一直使用的是索引别名来操作,那么如果你想更换另一个索引,只需要修改索引别名引用到新的索引即可,程序代码不需要任何改变。
2、创建不同功能的索引:可以通过建立索引检索的数据窗口,来实现特定的功能,例如,如果你为某个数据每天建立一个索引,当你想查询一周的数据时,你可以创建一个一周的索引别名,然后把每天的索引添加到这个别名中,只保留7天的数据,超过7天则删除最早的。建立索引别名之前,先插入3条数据
PUT user_info/_doc/1{ "name":"张三", "age":18, "address":"中国南京市鼓楼区", "tel":"13901234567"}PUT user_info/_doc/2{ "name":"李四", "age":19, "address":"中国南京市建邺区", "tel":"13901234568"}PUT user_info/_doc/3{ "name":"王五", "age":20, "address":"中国北京市朝阳区", "tel":"13901234567"}
PUT /user_info/_alias/user_info_alias_1
格式为:原索引名/_alias/别名
GET /user_info_alias_1/_search
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "user_info", "_type": "_doc", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 19, "address": "中国南京市建邺区", "tel": "13901234568" } }, { "_index": "user_info", "_type": "_doc", "_id": "1", "_score": 1, "_source": { "name": "张三", "age": 18, "address": "中国南京市鼓楼区", "tel": "13901234567" } }, { "_index": "user_info", "_type": "_doc", "_id": "3", "_score": 1, "_source": { "name": "王五", "age": 20, "address": "中国北京市朝阳区", "tel": "13901234567" } } ] }}
PUT /user_info/_alias/user_info_alias_age{ "filter": { "range": { "age": { "lte": 19 } } }}
查询结果
GET /user_info_alias_age/_search
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "user_info", "_type": "_doc", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 19, "address": "中国南京市建邺区", "tel": "13901234568" } }, { "_index": "user_info", "_type": "_doc", "_id": "1", "_score": 1, "_source": { "name": "张三", "age": 18, "address": "中国南京市鼓楼区", "tel": "13901234567" } } ] }}
POST /_aliases{ "actions": [ { "remove": { "index": "user_info", "alias": "user_info_alias_age" } } ]}
也可以通过这个方式建立一个索引
POST /_aliases{ "actions": [ { "add": { "index": "user_info", "alias": "user_info_alias_age", "filter": { "range": { "age": { "gte": 10, "lt": 20 } } } } } ]}
HEAD /_alias/user_info_alias_age
让索引别名指向一个新的索引:user_info_2
新的索引中插入3条数据,年龄分别加了10岁
PUT user_info_2/_doc/1{ "name":"张三", "age":28, "address":"中国南京市鼓楼区", "tel":"13901234567"}PUT user_info_2/_doc/2{ "name":"李四", "age":29, "address":"中国南京市建邺区", "tel":"13901234568"}PUT user_info_2/_doc/3{ "name":"王五", "age":30, "address":"中国北京市朝阳区", "tel":"13901234567"}
更新索引别名的指向
POST /_aliases{ "actions": [ { "add": { "index": "user_info_2", "alias": "user_info_alias_age" } }, { "remove": { "index": "user_info", "alias": "user_info_alias_age" } } ]}
再次查询
GET /user_info_alias_age/_search
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "user_info_2", "_type": "_doc", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 29, "address": "中国南京市建邺区", "tel": "13901234568" } }, { "_index": "user_info_2", "_type": "_doc", "_id": "1", "_score": 1, "_source": { "name": "张三", "age": 28, "address": "中国南京市鼓楼区", "tel": "13901234567" } }, { "_index": "user_info_2", "_type": "_doc", "_id": "3", "_score": 1, "_source": { "name": "王五", "age": 30, "address": "中国北京市朝阳区", "tel": "13901234567" } } ] }}
可以将别名指向的索引关联为写入索引。指定后,针对指向多个索引的别名的所有索引和更新请求将尝试解析为写入索引的一个索引。每个别名一次只能指定一个索引作为写入索引。如果未指定写入索引并且别名引用了多个索引,则将不允许写入。
可以使用别名 API 和索引创建 API 将与别名关联的索引指定为写入索引。
指定只能向user_info写入数据,user_info_2中并不会添加。
POST /_aliases{ "actions": [ { "add": { "index": "user_info", "alias": "user_info_alias_2", "is_write_index":true } }, { "add": { "index": "user_info_2", "alias": "user_info_alias_2" } } ]}
PUT /user_info_alias_2/_doc/5{ "name":"赵六", "age":31, "address":"中国南京市雨花区", "tel":"13901234569"}
相当于执行如下语句
PUT user_info/_doc/5{ "name":"赵六", "age":31, "address":"中国南京市雨花区", "tel":"13901234569"}
如果指定多个is_write_index为true
POST /_aliases{ "actions": [ { "add": { "index": "user_info", "alias": "user_info_alias_2", "is_write_index":true } }, { "add": { "index": "user_info_2", "alias": "user_info_alias_2", "is_write_index":true } } ]}
报错提示只能有一个is_write_index为true
{ "error": { "root_cause": [ { "type": "illegal_state_exception", "reason": "alias [user_info_alias_2] has more than one write index [user_info,user_info_2]" } ], "type": "illegal_state_exception", "reason": "alias [user_info_alias_2] has more than one write index [user_info,user_info_2]" }, "status": 500}
转载地址:http://yelrb.baihongyu.com/