📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

获取集群的状态信息,包括元数据、节点信息、路由表、阻塞信息等。

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指定要获取的状态指标。可选值:versionmaster_nodeblocksnodesmetadatarouting_tablerouting_nodescustoms_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": []
  }
}