创建索引时指定多个分片
示例:
PUT student
{
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 8
}
}
}
哈希算法
按照官方的讨论 ,使用了 murmur3 哈希算法。 作为 ES 使用者,不需要关注具体的算法,因为不保证以后不会换成其他的算法。
routing 参数
在文档相关的增删改查API 中,均可指定 routing 参数进行分片路由。
查询结果中展示文档所属分片
创建索引:
PUT student
{
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 8
}
}
}
插入数据时指定 routing 用于路由分片:
POST student/_doc/1?routing=1
{
"name": "name01"
}
POST student/_doc/2?routing=abc
{
"name": "name02"
}
在查询中指定 explain 为 true,可以看到文档的 shard 信息:
GET student/_search
{
"explain": true,
"query": {
"match_all": {}
}
}
结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 8,
"successful" : 8,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_shard" : "[student][0]",
"_node" : "wFhSfuLwR3OX21eldbRIHg",
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"name" : "name01"
},
"_explanation" : {
"value" : 1.0,
"description" : "*:*",
"details" : [ ]
}
},
{
"_shard" : "[student][5]",
"_node" : "wFhSfuLwR3OX21eldbRIHg",
"_index" : "student",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_routing" : "abc",
"_source" : {
"name" : "name02"
},
"_explanation" : {
"value" : 1.0,
"description" : "*:*",
"details" : [ ]
}
}
]
}
}
查询每个分片的信息
示例:
GET /student/_search_shards
如何指定查询某个分片中的数据
查询 shard 0 :
GET student/_search?preference=_shards:0
结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"name" : "name01"
}
}
]
}
}
查询 shard 1:
GET student/_search?preference=_shards:1
结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}