ES学习之路-别名
Elasticsearch中的API在针对特定索引时接受索引名,在适用时接受多个索引。index aliases API允许使用名称对索引进行别名,所有API都会自动将别名转换为实际的索引名。别名也可以映射到多个索引,并且在指定别名时,别名将自动展开到别名索引。别名还可以与筛选器相关联,筛选器在搜索和路由值时将自动应用。别名不能具有与索引相同的名称。
简单API样例:
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } }
]
}//给test1 index 添加一个别名 alias1
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } }
]
}//移除test1 index 的别名 alias1
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}//从test1索引中移除别名alias1,然后在test2索引中添加别名alias1
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}//分别给index test1,test2 添加索引alias1
POST /_aliases
{
"actions" : [
{ "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
]
}//分别给index test1,test2 添加索引alias1
Filtered Aliases
带有过滤器的别名提供了一种创建相同索引的不同“视图”的简单方法。过滤器可以使用查询DSL定义,并应用于所有搜索、计数、按查询删除等操作,更类似于使用此别名的操作。
如:
PUT /test1
{
"mappings": {
"type1": {
"properties": {
"user" : {
"type": "keyword"
}
}
}
}
}
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test1",
"alias" : "alias2",
"filter" : { "term" : { "user" : "kimchy" } }
}
}
]
}
Routing
可以将路由值与别名进行关联。这一功能可以用以过滤别名一起使用,避免不必要的碎片操作。
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias1",
"routing" : "1"
}
}
]
}
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias2",
"search_routing" : "1,2",
"index_routing" : "2"
}
}
]
}//搜索路由可有多个值, 但是index_routing只能有一个值
可以使用一个更为简单的方式创建索引,用下边这种方式不用记太多的语法
PUT /users
{
"mappings" : {
"user" : {
"properties" : {
"user_id" : {"type" : "integer"}
}
}
}
}
PUT /users/_alias/user_12
{
"routing" : "12",
"filter" : {
"term" : {
"user_id" : 12
}
}
}
//给users索引定义一个别名为user_12
也可以在创建索引的时候就创建一个别名,样例:
PUT /logs_20162801
{
"mappings" : {
"type" : {
"properties" : {
"year" : {"type" : "integer"}
}
}
},
"aliases" : {
"current_day" : {},
"2016" : {
"filter" : {
"term" : {"year" : 2016 }
}
}
}
}
//创建索引logs_20162801的同时创建两个别名,分别是current_day 和 2016
别名删除:
DELETE /logs_20162801/_alias/current_day
//删除logs_20162801 的别名 current_day
GET /logs_20162801/_alias/*
//删除logs_20162801 的所有别名
样例来源:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html