常見說法是下面三個字段在一個ES實例/集群中是全局唯一的:
index + type + 文檔 _id
但是實際上是:
index + type + 分片標識 + 文檔 _id
以下為驗證:
在 Elasticsearch 7 中創建有10個分片的 index:
PUT student
{
"mappings" : {
"properties" : {
"uid": {
"type" : "integer"
},
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 10,
"number_of_replicas" : 1
}
}
}
添加記錄1:
POST student/_doc/1?routing=1
{
"uid": 1,
"name": "張三",
"age": 10
}
查詢中帶上指定 explain 為 true,響應中能看到文檔屬於哪個 shard:
# 請求
GET student/_search
{
"query": {
"match": {
"uid": 1
}
},
"explain": true
}
# 響應
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_shard" : "[student][8]",
"_node" : "wFhSfuLwR3OX21eldbRIHg",
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"uid" : 1,
"name" : "張三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "uid:[1 TO 1]",
"details" : [ ]
}
}
]
}
}
添加記錄2:
POST student/_doc/1?routing=2
{
"uid": 1,
"name": "張三",
"age": 10
}
注意,和記錄1相比,除了 routing ,其他均沒有變化。
我們再次查詢_id
為1的記錄,會發現有兩條,唯一區別是 _shard
和 _routing
值不相同:
# 請求
GET student/_search
{
"query": {
"match": {
"uid": 1
}
},
"explain": true
}
# 響應
{
"took" : 565,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_shard" : "[student][7]",
"_node" : "wFhSfuLwR3OX21eldbRIHg",
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "2",
"_source" : {
"uid" : 1,
"name" : "張三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "uid:[1 TO 1]",
"details" : [ ]
}
},
{
"_shard" : "[student][8]",
"_node" : "wFhSfuLwR3OX21eldbRIHg",
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"uid" : 1,
"name" : "張三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "uid:[1 TO 1]",
"details" : [ ]
}
}
]
}
}