获取集群的状态信息,包括元数据、节点信息、路由表、阻塞信息等。
API #
GET /_cluster/state
GET /_cluster/state/{metric}
GET /_cluster/state/{metric}/{indices}
API 的作用 #
该 API 用于获取集群的状态信息,可以返回集群的各个方面的状态。
可用的状态指标(metric) #
| 指标 | 描述 |
|---|---|
version | 集群版本 |
master_node | 主节点信息 |
blocks | 阻塞信息 |
nodes | 节点信息 |
metadata | 元数据(索引、映射、别名等) |
routing_table | 路由表(分片分配信息) |
routing_nodes | 路由节点(节点上的分片分布) |
customs | 自定义数据 |
_all | 所有指标(默认) |
API 的参数 #
路由参数 #
| 参数 | 类型 | 是否必填 | 默认值 | 描述 |
|---|---|---|---|---|
{metric} | 字符串 | 否 | _all | 指定要获取的状态指标。可选值:version、master_node、blocks、nodes、metadata、routing_table、routing_nodes、customs、_all |
{indices} | 字符串 | 否 | _all | 指定要查询的索引名称,多个索引用逗号分隔 |
Query String 参数 #
| 参数 | 类型 | 是否必填 | 默认值 | 描述 |
|---|---|---|---|---|
local | 布尔值 | 否 | false | 是否从本地节点获取状态,而不是从主节点获取 |
master_timeout | 时间值 | 否 | 30s | 等待主节点响应的超时时间 |
wait_for_metadata_version | 整数 | 否 | 无 | 等待元数据达到指定版本 |
wait_for_timeout | 时间值 | 否 | 1m | 等待元数据版本超时时间 |
flat_settings | 布尔值 | 否 | false | 是否以扁平格式返回设置 |
pretty | 布尔值 | 否 | false | 是否格式化输出结果 |
示例 #
获取所有状态信息 #
GET /_cluster/state
仅获取节点信息 #
GET /_cluster/state/nodes
响应示例:
{
"cluster_name": "easysearch-cluster",
"cluster_uuid": "xxx-xxx-xxx",
"nodes": {
"node1_id": {
"name": "node-1",
"transport_address": "192.168.1.1:9300",
"attributes": {}
}
}
}
获取元数据 #
GET /_cluster/state/metadata
响应示例:
{
"cluster_name": "easysearch-cluster",
"cluster_uuid": "xxx-xxx-xxx",
"metadata": {
"indices": {
"my_index": {
"settings": {
"index": {
"creation_date": "1641234567890",
"number_of_shards": "5",
"number_of_replicas": "1"
}
},
"mappings": {},
"aliases": []
}
}
}
}
获取索引的路由信息 #
GET /_cluster/state/routing_table/my_index
响应示例:
{
"cluster_name": "easysearch-cluster",
"routing_table": {
"indices": {
"my_index": {
"shards": {
"0": [
{
"primary": true,
"state": "STARTED",
"node": "node1",
"relocating_node": null
}
]
}
}
}
}
}
获取多个索引的元数据 #
GET /_cluster/state/metadata/index1,index2
从本地获取集群状态 #
GET /_cluster/state?local=true
等待元数据版本 #
GET /_cluster/state?wait_for_metadata_version=123&wait_for_timeout=30s
获取主节点信息 #
GET /_cluster/state/master_node
响应示例:
{
"cluster_name": "easysearch-cluster",
"master_node": "node1_id"
}
获取阻塞信息 #
GET /_cluster/state/blocks
响应示例:
{
"cluster_name": "easysearch-cluster",
"blocks": {
"indices": {
"my_index": {
"0": {
"description": "index blocked",
"blocked": true
}
}
}
}
}
获取路由节点信息 #
GET /_cluster/state/routing_nodes
响应示例:
{
"cluster_name": "easysearch-cluster",
"routing_nodes": {
"nodes": {
"node1_id": [
{
"index": "my_index",
"shard": 0,
"primary": true,
"state": "STARTED"
}
]
},
"unassigned": []
}
}





