ES集群索引分片管理
- 分片(shard):因为ES是个分布式的搜索引擎,所以索引通常都会分解成不同部分,而这些分布在不同节点的数据就是分片,ES自动管理和组织分片,并在必要的时候对分片数据进行再平衡分配,所以用户基本上不用担心分片的处理细节。
- 副本(replica):ES默认为一个索引创建一个主分片,并分别为其创建一个副分片,也就是说每个索引都由一个主分片成本,而每个主分片都有相应的一个copy。
- ES7.x之后,如果不指定索引分片,默认会创建一个主分片和一个副分片,而7.x版本之前的比较,如6.x版本,默认是5个主分片
创建索引(不指定分片数量)
curl -X PUT "172.25.45.150:9200/nba" -H 'Content-Type:aplication/json' -d '
{
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
'
curl -X GET "172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646360464892",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "oUWzENVEQVW_hXZlCdfQhg",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
curl -X DELETE "172.25.45.150:9200/nba"
创建索引(指定分片数量)
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
curl -X PUT "172.25.45.150:9200/nba" -H 'Content-Type:aplication/json' -d '
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
'
curl -X GET "172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646361232608",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "m_z801WySBCVX-ujrGSv8g",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
索引分片分配
- 分片分配到哪个节点是由ES自动管理的,如果某个节点挂了,那分配又会重新分配到别的节点上。
- 在单机中,节点没有副分片,因为只有一个节点没必要生成分片,一个节点挂了,副分片也会挂掉,完全是单故障,没有存在的意义。
- 在集群中,同个分片它的主分片和副分片不会在同一个节点上,因为主分片和副分片在同个节点,节点挂了,副分片和主分片一样是挂了,不要把鸡蛋都放在同个篮子里。
- 可以手动移动分片,比如把某个分片移动从节点1移动到节点2.
- 创建索引时指定的主分片数以后是无法修改的,所以主分片数的数量要根据项目决定,如果真的要增加主分片只能重建索引了。副分片数以后是可以修改的。

手动移动分片
curl -X PUT "172.25.45.150:9200/_cluster/reroute" -H 'Content-Type:aplication/json' -d '
{
"commands": [
{
"move": {
"index": "nba",
"shard": 2,
"from_node": "node-1",
"to_node": "node-3"
}
}
]
}
'

修改副分片数量
curl -X PUT "http://172.25.45.150:9200/nba/_settings" -H 'Content-Type:application/json' -d '
{
"number_of_replicas":2
}
'
curl -X GET "http://172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646361232608",
"number_of_shards": "3",
"number_of_replicas": "2",
"uuid": "m_z801WySBCVX-ujrGSv8g",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
