别名
edit别名
edit别名是数据流或索引组的次要名称。大多数 Elasticsearch API 接受别名来代替数据流或索引名称。
您可以随时更改别名的数据流或索引。如果在应用程序的Elasticsearch请求中使用别名,您可以在不停机或不更改应用程序代码的情况下重新索引数据。
别名类型
edit有两种类型的别名:
- 一个数据流别名指向一个或多个数据流。
- 一个索引别名指向一个或多个索引。
别名不能同时指向数据流和索引。您也不能将数据流的备份索引添加到索引别名中。
添加别名
edit要将现有数据流或索引添加到别名中,请使用别名 API的add操作。如果别名不存在,请求将创建它。
POST _aliases
{
"actions": [
{
"add": {
"index": "logs-nginx.access-prod",
"alias": "logs"
}
}
]
}
API 的 index 和 indices 参数支持通配符 (*)。匹配数据流和索引的通配符模式会返回错误。
POST _aliases
{
"actions": [
{
"add": {
"index": "logs-*",
"alias": "logs"
}
}
]
}
移除别名
edit要删除别名,请使用别名 API 的 remove 操作。
POST _aliases
{
"actions": [
{
"remove": {
"index": "logs-nginx.access-prod",
"alias": "logs"
}
}
]
}
多个操作
edit您可以使用别名 API 在单个原子操作中执行多个操作。
例如,logs 别名指向一个单一的数据流。以下请求将别名的流进行交换。在此交换过程中,logs 别名没有停机时间,并且永远不会同时指向两个流。
POST _aliases
{
"actions": [
{
"remove": {
"index": "logs-nginx.access-prod",
"alias": "logs"
}
},
{
"add": {
"index": "logs-my_app-default",
"alias": "logs"
}
}
]
}
多个操作结果
edit当使用多个操作时,如果某些操作成功而某些操作失败,将返回每个操作结果的列表。
考虑一个与前一个示例类似的动作列表,但现在使用了一个尚不存在的别名 log-non-existing。
在这种情况下,remove 动作将会失败,但 add 动作将会成功。
响应将包含列表 action_results,其中包含每个请求动作的结果。
POST _aliases
{
"actions": [
{
"remove": {
"index": "index1",
"alias": "logs-non-existing"
}
},
{
"add": {
"index": "index2",
"alias": "logs-non-existing"
}
}
]
}
API返回以下结果:
{
"acknowledged": true,
"errors": true,
"action_results": [
{
"action": {
"type": "remove",
"indices": [ "index1" ],
"aliases": [ "logs-non-existing" ],
},
"status": 404,
"error": {
"type": "aliases_not_found_exception",
"reason": "aliases [logs-non-existing] missing",
"resource.type": "aliases",
"resource.id": "logs-non-existing"
}
},
{
"action": {
"type": "add",
"indices": [ "index2" ],
"aliases": [ "logs-non-existing" ],
},
"status": 200
}
]
}
允许操作列表部分成功可能不会提供期望的结果。
将 must_exist 设置为 true 可能更为合适,这将导致如果单个操作失败,整个操作列表都会失败。
在索引创建时添加别名
edit您还可以使用组件模板或 索引模板在创建索引或数据流时添加索引或数据流别名。
# Component template with index aliases
PUT _component_template/my-aliases
{
"template": {
"aliases": {
"my-alias": {}
}
}
}
# Index template with index aliases
PUT _index_template/my-index-template
{
"index_patterns": [
"my-index-*"
],
"composed_of": [
"my-aliases",
"my-mappings",
"my-settings"
],
"template": {
"aliases": {
"yet-another-alias": {}
}
}
}
您也可以在创建索引 API请求中指定索引别名。
# PUT <my-index-{now/d}-000001>
PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
{
"aliases": {
"my-alias": {}
}
}
视图别名
edit要获取集群别名列表,请使用不带参数的获取别名 API。
GET _alias
在_alias之前指定一个数据流或索引以查看其别名。
GET my-data-stream/_alias
在 _alias 后指定一个别名以查看其数据流或索引。
GET _alias/logs
写索引
edit您可以使用 is_write_index 来指定别名的写入索引或数据流。Elasticsearch 会将别名的任何写入请求路由到此索引或数据流。
POST _aliases
{
"actions": [
{
"add": {
"index": "logs-nginx.access-prod",
"alias": "logs"
}
},
{
"add": {
"index": "logs-my_app-default",
"alias": "logs",
"is_write_index": true
}
}
]
}
如果一个别名指向多个索引或数据流,并且未设置is_write_index,则该别名会拒绝写请求。如果一个索引别名指向一个索引,并且未设置is_write_index,则该索引会自动充当写索引。即使别名指向一个数据流,数据流别名也不会自动设置写数据流。
我们建议使用数据流来存储仅追加的时间序列数据。如果您需要更新或删除现有时间序列数据,可以直接对数据流支持的索引执行更新或删除操作。如果您经常使用相同的_id发送多个文档,期望最后写入胜出,您可能希望使用带有写入索引的索引别名。请参阅不使用数据流管理时间序列数据。
过滤一个别名
edit选项 filter 使用 Query DSL 来限制别名可以访问的文档。
POST _aliases
{
"actions": [
{
"add": {
"index": "my-index-2099.05.06-000001",
"alias": "my-alias",
"filter": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": "now-1d/d",
"lt": "now/d"
}
}
},
{
"term": {
"user.id": "kimchy"
}
}
]
}
}
}
}
]
}
路由
edit使用 routing 选项将别名的请求路由到特定的分片。这使您能够利用 分片缓存 来加速搜索。数据流别名不支持路由选项。
POST _aliases
{
"actions": [
{
"add": {
"index": "my-index-2099.05.06-000001",
"alias": "my-alias",
"routing": "1"
}
}
]
}
使用 index_routing 和 search_routing 来为索引和搜索指定不同的路由值。如果指定,这些选项会覆盖它们各自操作的 routing 值。
POST _aliases
{
"actions": [
{
"add": {
"index": "my-index-2099.05.06-000001",
"alias": "my-alias",
"search_routing": "1",
"index_routing": "2"
}
}
]
}