Elasticsearch如何查询主分片所在运行节点?含分片配置场景
Hey there! Let's break down your Elasticsearch questions clearly and practically:
The easiest way to get this info is using the handy _cat/shards API—it’s built for human-readable cluster shard details. Just run this command in Kibana Dev Tools or via curl:
GET _cat/shards?v
In the output, look for rows where the prirep column shows a p (that’s the marker for primary shards). The last column (node) will display the exact name of the node where that primary shard is running.
If you want to cut straight to primary shards without extra noise, add a filter to the query:
GET _cat/shards?v&h=index,shard,prirep,node&prirep=p
This trims the output to only the columns you care about: index name, shard ID, primary status, and the hosting node.
For a specific index, narrow down the _cat/shards API to target just that index. Replace <your-index-name> with your actual index’s name:
GET _cat/shards/<your-index-name>?v
Focus on rows with prirep=p—you’ll see 5 entries (one for each primary shard, IDs 0-4), and the node column will tell you which of your 3 cluster nodes each primary is hosted on.
If you prefer structured JSON output (perfect for scripting), use the _cluster/state API with a filter to pull only primary shard routing data for your index:
GET _cluster/state?filter_path=routing_table.indices.<your-index-name>.shards.*.primary
A simplified response will look like this:
{ "routing_table": { "indices": { "your-index-name": { "shards": { "0": { "primary": { "node": "node-1" } }, "1": { "primary": { "node": "node-2" } }, // ... shards 2-4 will follow with their respective node assignments } } } } }
This gives you a clean, machine-readable view of exactly which node each primary shard is assigned to.
内容的提问来源于stack exchange,提问作者Klaus

