05_ES教程之解决unassigned shards问题
如何处理elastic search中的unassigned shards?
一. 查看节点分片信息GET _cat/allocation?v
我们通过 GET _cat/allocation?v
可以查看每个节点分片的分配数量以及它们所使用的硬盘空间大小 
可能会发现其有5个shard是unassigned状态.
二. 查看集群健康状态 1. 查看健康状态GET _cat/health?v
再通过命令GET _cat/health?v
查看集群健康状态 
可以看到其active_shards_percent为87.8%,elasticsearch健康状态为yellow,原因就是其存在UNASSIGNED shards的情况,不过即使存在unassigned shard,也并不会影响es的使用的.
2. 产生原因产生unassigned shards的原因是什么呢?
如果我们只有一台机器,部署运行了es,但是却在index的settings中设置了replica为1,那么这个replica shard就会成为unassigned shards,因为分片不能分配到已经存在分片副本的同一节点.
而且你在查看原因的时候,其会显示:
the shard cannot be allocated to the same node on which a copy of the shard already exists
即分片不能分配到已经存在分片副本的同一节点!!!
3. 首先精确定位unassigned shard的位置GET _cat/shards?h=index,shard,prirep,state,unassigned.reason | grep UNASSIGNED

每行列出索引的名称,分片编号,是主分片p还是副本分片r,以及其未分配的原因.
可以看到索引order的replica设置为1,而且是5个shards.
4. 可以通过以下语句查看具体原因GET _cluster/allocation/explain?pretty

DELETE order(索引名称)
5.2 对正在使用中的索引,可以设置index的replica为0
PUT order/_settings
{
"number_of_replicas": 0
}
5.3 通过循环获取存在unassigned shard的index name
GET _cluster/allocation/explain?pretty
5.4 设置复制分片数量为0
PUT order/_settings
{
"number_of_replicas": 0
}
 
来消除unassigned shards.

此时可以看到,我们的es节点健康状态为green,非常健康(绿绿更健康,?)!!!