# Search Shards API

原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html)

译文链接 : [http://apache.wiki/display/Elasticsearch/Search+Shards+API](http://apache.wiki/display/Elasticsearch/Search+Shards+API)

贡献者 : @Josh

搜索分片（**Shards**）**API **返回将执行搜索请求的索引和分片。 这可以提供有用的反馈用于解决问题或者计划用 **routing** 和 **shard preference** 来优化。

**index** 和 **type** 参数可以为单个值，或者用逗号分隔。

## 用法

比如：

```
GET /twitter/_search_shards
```

产生如下结果：

```
{
  "nodes": ...,
  "shards": [
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 0,
        "state": "STARTED",
        "allocation_id": {"id":"0TvkCyF7TAmM1wHP4a42-A"},
        "relocating_node": null
      }
    ],
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 1,
        "state": "STARTED",
        "allocation_id": {"id":"fMju3hd1QHWmWrIgFnI4Ww"},
        "relocating_node": null
      }
    ],
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 2,
        "state": "STARTED",
        "allocation_id": {"id":"Nwl0wbMBTHCWjEEbGYGapg"},
        "relocating_node": null
      }
    ],
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 3,
        "state": "STARTED",
        "allocation_id": {"id":"bU_KLGJISbW0RejwnwDPKw"},
        "relocating_node": null
      }
    ],
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 4,
        "state": "STARTED",
        "allocation_id": {"id":"DMs7_giNSwmdqVukF7UydA"},
        "relocating_node": null
      }
    ]
  ]
}
```

指定相同的请求，这次用一个 **routing** 值：

```
GET /twitter/_search_shards?routing=foo,baz
```

这将获得以下结果：

```
{
  "nodes": ...,
  "shards": [
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 0,
        "state": "STARTED",
        "allocation_id": {"id":"0TvkCyF7TAmM1wHP4a42-A"},
        "relocating_node": null
      }
    ],
    [
      {
        "index": "twitter",
        "node": "JklnKbD7Tyqi9TP3_Q_tBg",
        "primary": true,
        "shard": 1,
        "state": "STARTED",
        "allocation_id": {"id":"fMju3hd1QHWmWrIgFnI4Ww"},
        "relocating_node": null
      }
    ]
  ]
}
```

## 所有参数

| **routing** | 以逗号分隔的路由值列表考虑到当确定哪些碎片会反对执行的请求。 |
| **preference** | 控制要对其执行搜索请求的分片副本的 **perference** 。 默认情况下，操作在分片副本之间随机化。 有关所有可接受值的列表，请参阅 [preference](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-request-preference.html) 文档。 |
| **local** | 一个布尔值，是否在本地读取集群状态以便确定在何处分配碎片，而不是使用Master节点的集群状态。 |