查询条件中追加?preference=_shards:<num>
接口。
示例
数据准备
# 创建索引
PUT student
{
"mappings" : {
"properties" : {
"uid": {
"type" : "integer"
},
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 10,
"number_of_replicas" : 1
}
}
}
# 插入数据
POST student/_doc/1?routing=1
{
"uid": 1,
"name": "张三",
"age": 10
}
# 插入数据
POST student/_doc/2?routing=2
{
"uid": 2,
"name": "张三",
"age": 10
}
查询数据所在 shard
查询中增加"explain": true
即可。
请求:
GET student/_search
{
"query": {
"match_all": {}
},
"size": 20,
"explain": true
}
响应:
{
"took" : 137,
"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]", # 所属 shard
"_node" : "PI2YKOcTToeceMtdvgdUaw",
"_index" : "student",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_routing" : "2",
"_source" : {
"uid" : 2,
"name" : "张三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "*:*",
"details" : [ ]
}
},
{
"_shard" : "[student][8]",
"_node" : "PI2YKOcTToeceMtdvgdUaw",
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"uid" : 1,
"name" : "张三",
"age" : 10
},
"_explanation" : {
"value" : 1.0,
"description" : "*:*",
"details" : [ ]
}
}
]
}
}
查询指定 shard 中的数据
请求:
GET student/_search?preference=_shards:1
{
"query": {
"match_all": {}
},
"size": 20
}
响应为空。
请求:
GET student/_search?preference=_shards:7
{
"query": {
"match_all": {}
},
"size": 20
}
响应:
{
"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" : "2",
"_score" : 1.0,
"_routing" : "2",
"_source" : {
"uid" : 2,
"name" : "张三",
"age" : 10
}
}
]
}
}