分片数量至少为1,副本数量至少为 0 。
默认分片数、副本数
创建名为 movie 的索引 :
PUT movie
注意,PUT movie
和 PUT /movie
效果是一样的。
响应:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "movie"
}
查询索引信息:
GET movie
响应:
{
"movie" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1572843447230",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "AzEtRiBaQqO5JDN_l7WG-w",
"version" : {
"created" : "7020099"
},
"provided_name" : "movie"
}
}
}
}
可以看到,分片数量number_of_shards
为1,副本数量为number_of_replicas
1 。
修改副本数量
请求:
PUT movie/_settings
{
"index" : {
"number_of_replicas" : 3
}
}
响应:
{
"acknowledged" : true
}
注意,当副本创建完成后(可能耗时很长),才会响应。如果耗时很长,可以不用等待响应。过一段时间后去查询索引信息确认即可。
修改分片数量
分片数量,必须在索引创建时指定,创建后无法修改。尝试修改时会报错:
# 请求
PUT movie/_settings
{
"index" : {
"number_of_shards" : 3
}
}
# 响应
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[movie/BPnIxSCwTvWNtze-4gDJaw]]"
}
],
"type": "illegal_argument_exception",
"reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[movie/BPnIxSCwTvWNtze-4gDJaw]]"
},
"status": 400
}
创建索引时指定分片数量、副本数量
删除之前创建的索引:
DELETE movie
重新创建 movie 索引:
PUT movie
{
"settings" : {
"index" : {
"number_of_shards" : 2,
"number_of_replicas" : 3
}
}
}
响应:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "movie"
}
查看索引信息:
# 请求
GET movies
# 响应
{
"movie" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1572921604021",
"number_of_shards" : "2",
"number_of_replicas" : "3",
"uuid" : "BPnIxSCwTvWNtze-4gDJaw",
"version" : {
"created" : "7020099"
},
"provided_name" : "movie"
}
}
}
}