加载中

别名

别名指向一个或多个索引或数据流。大多数 Elasticsearch API 接受使用别名来代替数据流或索引名称。

通过别名,您可以:

  • 使用单个名称同时查询多个索引/数据流
  • 实时更改应用程序使用的索引/数据流
  • 重建索引数据而无需停机

别名有两种类型

  • 数据流别名指向一个或多个数据流。
  • 索引别名指向一个或多个索引。

别名不能同时指向数据流和索引。您也不能将数据流的后备索引添加到索引别名中。

要将现有的数据流或索引添加到别名,请使用 aliases APIadd 操作。如果该别名不存在,请求会创建它。

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

API 的 indexindices 参数支持通配符 (*)。同时匹配数据流和索引的通配符模式会报错。

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

要删除别名,请使用 aliases API 的 remove 操作。

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

您可以使用 aliases API 在单个原子操作中执行多个操作。

例如,logs 别名指向单个数据流。以下请求会交换别名指向的数据流。在此交换期间,logs 别名不会停机,也不会同时指向两个数据流。

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

使用多个操作时,如果某些成功而某些失败,将返回一个各操作结果的列表。

考虑与上一个示例类似的操作列表,但现在使用一个尚不存在的别名 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 可能更合适,这将导致如果单个操作失败,整个操作列表都会失败。

您还可以使用 组件模板索引模板,以便在创建索引或数据流时添加它们的别名。

				
					# 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": {}
  }
}
		

要获取集群别名列表,请使用不带参数的 get alias API

				GET _alias
		

_alias 之前指定数据流或索引以查看其别名。

				GET my-data-stream/_alias
		

_alias 之后指定别名以查看其数据流或索引。

				GET _alias/logs
		

您可以使用 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,则该索引会自动充当写入索引。即使别名只指向一个数据流,数据流别名也不会自动设置写入数据流。

提示

我们建议使用数据流来存储仅追加(append-only)的时间序列数据。如果您需要更新或删除现有的时间序列数据,可以直接对数据流的后备索引执行更新或删除操作。如果您经常发送使用相同 _id 的多个文档并期望“最后写入者胜”(last-write-wins),则可能需要改用带有写入索引的索引别名。请参阅教程 在没有数据流的情况下管理时间序列数据

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"
                }
              }
            ]
          }
        }
      }
    }
  ]
}
		
注意

过滤器仅在使用 Query DSL 时应用,而在通过 ID 检索文档时则不应用。

使用 routing 选项将别名的请求路由到特定分片。这使您可以利用分片缓存来加速搜索。数据流别名不支持路由选项。

				POST _aliases
					{
  "actions": [
    {
      "add": {
        "index": "my-index-2099.05.06-000001",
        "alias": "my-alias",
        "routing": "1"
      }
    }
  ]
}
		

使用 index_routingsearch_routing 为索引和搜索指定不同的路由值。如果指定,这些选项将覆盖其各自操作的 routing 值。

				POST _aliases
					{
  "actions": [
    {
      "add": {
        "index": "my-index-2099.05.06-000001",
        "alias": "my-alias",
        "search_routing": "1",
        "index_routing": "2"
      }
    }
  ]
}
		

要移除索引,请使用 aliases API 的 remove_index 操作。

				POST _aliases
					{
  "actions": [
    {
      "remove_index": {
        "index": "my-index-2099.05.06-000001"
      }
    }
  ]
}
		
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.