一、索引别名
别名,有点类似数据库的视图,别名一般都会和一些过滤条件相结合,可以做到即使是同一个索引上,让不同人看到不同的数据。别名的访问接口是_alias。
1、创建索引添加别名
PUT /index_alias
{
"aliases": {
"index1": {
"filter": {
"term": {
"name": "Apache"
}
}
}
}
}
2、为已有索引添加别名
PUT /open-soft/_alias/open-soft-alias
{
"filter": {
"match": {
"lang": "Java8"
}
}
}
查询指定索引的别名:
GET /open-soft/_alias
删除别名:
DELETE /open-soft/_alias/open-soft-alias
3、查询别名:
GET _cat/aliases
get索引名/_alias,看索引所有的别名,如:GET /open-soft/_alias
get别名,看别名关联索引的详情,如:GET open-soft-alias
通过别名查询:
别名的使用和索引差不多,比如:
GET open-soft-alias/_search
4、_aliases接口
同时_aliases接口可以做批量操作,比如通过_aliases接口将一个别名关联多个索引:
POST /_aliases
{
"actions": [
{
"add": {
"index": "my_index",
"alias": "my_index_alias"
}
},
{
"add": {
"index": "my_index2",
"alias": "my_index_alias"
}
}
]
}
或者对于同一个index,我们给不同人看到不同的数据,如my_index有个字段是team,team字段记录了该数据是那个team的。team之间的数据是不可见的。
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的每个索引都使用别名,因为在未来重建索引的时候,别名会赋予你更多的灵活性。假设一开始创建的索引只有一个主分片,之后你又决定为索引扩容。如果为原有的索引使用的是别名,现在你可以修改别名让其指向额外创建的新索引,而无须修改被搜索的索引之名 称(假设一开始你就为搜索使用了别名)。
另一个有用的特性是,在不同的索引中创建窗口。比如,如果为数据创建了每日索引,你可能期望一个滑动窗口涵盖过去一周的数据,别名就称为last-7-days。然后,每天创建新的每日索引时,你可以将其加入别名,同时停用或者删除第8天前的旧索引。
别名还能提供另一个特性,那就是路由。不过,在谈论使用别名进行路由之前,我们先来讲述一下通常情况下路由是如何使用的。
5、结合路由和别名
在之前的章节中,我们已经了解到别名是索引之上的抽象,非常强大和灵活。假设别名指向一个单独的索引,那么它们也可以和路由一起使用,在查询或索引 的时候自动地使用路由值。如
POST /_aliases
{
"actions": [
{
"add": {
"index": "open-soft",
"alias": "my_index_alias",
"filter": {
"match": {
"lang": "Java"
}
},
"routing": "AKey"
}
},
{
"add": {
"index": "open-soft",
"alias": "my_index_alias2",
"filter": {
"match": {
"lang": "Java"
}
},
"routing": "BKe