简介:⼿把⼿教你索引分⽚管理
介绍
分⽚(shard):因为ES是个分布式的搜索引擎, 所以索引通常都会分解成不同部分, ⽽这些分布在不同节点的数据就是分⽚. ES⾃动管理和组织分⽚, 并在必要的时候对分⽚数据进⾏再平衡分配, 所以⽤户基本上不⽤担⼼分⽚的处理细节。副本(replica):ES默认为⼀个索引创建1个主分⽚, 并分别为其创建⼀个副本分⽚. 也就是说每个索引都由1个主分⽚成本, ⽽每个主分⽚都相应的有⼀个copy.Elastic search7.x之后,如果不指定索引分⽚,默认会创建1个主分⽚和⼀个副分⽚,⽽7.x版本之前的⽐如6.x版本,默认是5个主分⽚
创建索引(不指定分⽚数量)
PUT nba
{
"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"
}
}
}
}
创建索引(指定分⽚数量)
settings
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
创建索引
PUT nba
{
"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"
}
}
}
}
索引分⽚分配
分⽚分配到哪个节点是由ES⾃动管理的,如果某个节点挂了,那分⽚⼜会重新分配到别的节点上。在单机中,节点没有副分⽚,因为只有⼀个节点没必要⽣成副分⽚,⼀个节点挂点,副分⽚也会挂掉,完全是单故障,没有存在的意义。在集群中,同个分⽚它的主分⽚不会和它的副分⽚在同⼀个节点上,因为主分⽚和副分⽚在同个节点,节点挂了,副分⽚和主分机⼀样是挂了,不要把所有的鸡蛋都放在同个篮⼦⾥。可以⼿动移动分⽚,⽐如把某个分⽚移动从节点1移动到节点2。创建索引时指定的主分⽚数以后是⽆法修改的,所以主分⽚数的数量要根据项⽬决定,如果真的要增加主分⽚只能重建索引了。副分⽚数以后是可以修改的。
⼿动移动分⽚
POST /_cluster/reroute
{
"commands": [{
"move": {
"index": "nba",
"shard": 2,
"from_node": "node-1",
"to_node": "node-3"
}
}]
}
修改副分⽚数量
PUT /nba/_settings
{
"number_of_replicas": 2
}