资讯专栏INFORMATION COLUMN

Elasticsearch之别名

不知名网友 / 1949人阅读

摘要:如或者接口接口用于根据一系列条件将别名指向一个新的索引,这些条件包括存续时间文档数量和存储容量等。但这种别名滚动并不会自动完成,需要主动调用接口。

别名,有点类似数据库的视图,别名一般都会和一些过滤条件相结合,可以做到即使是同一个索引上,让不同人看到不同的数据。别名的访问接口是_alias。

创建索引时添加别名

</>复制代码

  1. PUT test_index_v1{ "aliases": { "test_index": {} }}

为已有索引添加别名

</>复制代码

  1. put test_index_v2put test_index_v2/_alias/test_index2

查询所有的别名

</>复制代码

  1. GET _alias{ "test_index_v1" : { "aliases" : { "test_index" : { } } }, "test_index_v2" : { "aliases" : { "test_index2" : { } } }}GET _cat/aliases?valias index filter routing.index routing.search is_write_index.kibana_task_manager .kibana_task_manager_1 - - - -.kibana .kibana_1 - - - -test_index test_index_v1 - - - -test_index2 test_index_v2 - - - -

查询指定索引的别名

</>复制代码

  1. GET test_index_v1/_alias{ "test_index_v1" : { "aliases" : { "test_index" : { } } }}

删除别名

</>复制代码

  1. delete test_index_v2/_alias/test_index2

通过别名查询

别名的使用和索引差不多,比如:

</>复制代码

  1. put study_route/_alias/srget sr/_search

批量操作

同时_aliases接口可以做批量操作,比如通过_aliases接口将一个别名关联多个索引:

</>复制代码

  1. POST _aliases{ "actions": [ { "add": { "index": "logs-nginx.access-prod", "alias": "logs" } } ]}

index and indices 参数还支持通配符*:

</>复制代码

  1. POST _aliases{ "actions": [ { "add": { "index": "logs-*", "alias": "logs" } } ]}

或者对于同一个index,我们给不同人看到不同的数据,如my_index有个字段是team,team字段记录了该数据是那个team 的。team之间的数据是不可见的。

</>复制代码

  1. POST /_aliases{ "actions": [ { "add": { "index": "my_index", "alias": "my_index_teamA_alias", "filter": { "term": { "team": "teamA" } } } }, { "add": { "index": "my_index", "alias": "my_index_teamB_alias", "filter": { "term": { "team": "teamB" } } } }, { "add": { "index": "my_index", "alias": "my_index_team_alias" } } ]}

只要有可能,尽量使用别名,推荐为Elasticsearch的每个索引都使用别名,因为在未来重建索引的时候,别名会赋予你更多的灵活性。假设一开始创建的索引只有一个主分片,之后你又决定为索引扩容。如果为原有的索引使用的是别名,现在你可以修改别名让其指向额外创建的新索引,而无须修改被搜索的索引之名称(假设一开始你就为搜索使用了别名)。

另一个有用的特性是,在不同的索引中创建窗口。比如,如果为数据创建了每日索引,你可能期望一个滑动窗口涵盖过去一周的数据,别名就称为last7days。然后,每天创建新的每日索引时,你可以将其加入别名,同时停用或者删除第8天前的旧索引。

别名还能提供另一个特性,那就是路由。假设别名指向一个多带带的索引,那么它们也可以和路由一起使用,在查询或索引
的时候自动地使用路由值。如:

</>复制代码

  1. POST /_aliases{ "actions": [ { "add": { "index": "test_index", "alias": "my_index_alias", "filter": { "match": { "lang": "Java" } }, "routing": "AKey" } }, { "add": { "index": "test_index", "alias": "my_index_alias2", "filter": { "match": { "lang": "Java" } }, "routing": "BKey" } } ]}

或者:

</>复制代码

  1. PUT test_index/_alias/ts{ "filter": { "match": { "lang": "Java" } }, "routing": "CKey"}

_rollover接口

_rollover接口用于根据一系列条件将别名指向一个新的索引,这些条件包括存续时间、文档数量和存储容量等。这与日志文件使用的文件滚动类似,文件滚动是通过不断创建新文件并滚动旧文件来保证日志文件不会过于庞大,而_rollover接口则是通过不断将别名指向新的索引以保证索引容量不会过大。但这种别名滚动并不会自动完成,需要主动调用_rollover接口。

别名滚动的条件可通过conditions参数设置,包括max_age、max_docs和max_size等三个子参数。

例如,创建一个索引logs-1并分配别名logs:

</>复制代码

  1. PUT logs-1{ "aliases": { "logs": {} }}

然后调用logs别名的_rollover接口设置别名滚动条件,如:

</>复制代码

  1. POST /logs/_rollover{ "conditions": { "max_age": "10s", "max_docs": 10000, "max_size": "4gb" }}{ "acknowledged" : true, "shards_acknowledged" : true, "old_index" : "logs-1", "new_index" : "logs-000002", "rolled_over" : true, "dry_run" : false, "conditions" : { "[max_size: 4gb]" : false, "[max_age: 10s]" : true, "[max_docs: 10000]" : false }}

在示例中,logs别名指向logs-1索引,最大存活周期为10s,最大文档数量10000条,最大存储容量4GB。为了演示效果,特意将最大存活周期设置为10秒,从返回结果的conditions属性来看,max_age这个条件返回true,所以会触发索引滚动。

通过_cat/indices接口就会发现有新的索引logs-000002产生:

</>复制代码

  1. get _cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.sizeyellow open logs-1 jjNxkUn1T1yD245qUH6SVQ 1 1 0 0 283b 283byellow open logs-000002 H85Y0qwQRnucttIc53t8CQ 1 1 0 0 230b 230b

再来看看别名:

</>复制代码

  1. get _cat/aliases?valias index filter routing.index routing.search is_write_indexlogs logs-000002 - - - -

logs-1的别名已经被清空,而logs-000002的别名中则已经添加了logs。新索引的命名规则在原索引名称数字的基础上加 1,并且将数值长度补0凑足6位。所以使用_rollover接口时,要求索引名称必须以数字结尾,数字与前缀之间使用连接线“-”连接。

由于_rollover接口在滚动新索引时,会将别名与原索引的关联取消,所以通过别名再想查找原文档就不可能了。为了保证原文档可检索,可以通过别名is_write_index参数保留索引与别名的关系。当使用is_write_index参数设置了哪一个索引为写索引时,_rollover接口滚动别名指向索引时将不会取消别名与原索引之间的关系。它会将原索引的is_write_index参数设置为false,并将新索引的is_write_index参数设置为true。

例如在创建logs-4时指定参数is_write_index为true:

</>复制代码

  1. PUT /logs-4{ "aliases": { "logs4": { "is_write_index": true } }}

然后调用_rollover接口:

</>复制代码

  1. POST logs4/_rollover{ "conditions": { "max_age": "10s", "max_docs": 10000, "max_size": "4gb" }}{ "acknowledged" : true, "shards_acknowledged" : true, "old_index" : "logs-4", "new_index" : "logs-000005", "rolled_over" : true, "dry_run" : false, "conditions" : { "[max_size: 4gb]" : false, "[max_age: 10s]" : true, "[max_docs: 10000]" : false }}

然后查询索引的别名:

</>复制代码

  1. GET _cat/aliases?valias index filter routing.index routing.search is_write_indexlogs4 logs-4 - - - falselogs4 logs-000005 - - - t

会发现logs-4的is_write_index参数被设置为false,而新生成索引logs-000005的is_write_index参数则为true。在两者的别名列表中都包含有logs4,可以继续通过logs别名对原索引进行查询。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/125636.html

相关文章

发表评论

0条评论

不知名网友

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<