ElasticSearch查看已经存在的文档保存在哪个分片

比如我有以下的文档:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "mydoc",
        "_id" : "XxyrM2kBVzdNcvl_GHv2",
        "_score" : 1.0,
        "_source" : {
          "name" : "Shiled",
          "twitter" : "Sonny sql is awesome",
          "date" : "2018-12-27",
          "id" : 1240,
          "tags" : [
            "red",
            "shit"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "mydoc",
        "_id" : "YByrM2kBVzdNcvl_tnvm",
        "_score" : 1.0,
        "_source" : {
          "name" : "YYerk",
          "twitter" : "sql is awesome",
          "date" : "2008-12-27",
          "id" : 12357,
          "tags" : [
            "red"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "mydoc",
        "_id" : "7777",
        "_score" : 1.0,
        "_source" : {
          "name" : "Rocky Chen",
          "twitter" : "sql is awesome",
          "date" : "2008-12-27",
          "id" : 9999
        }
      },
      {
        "_index" : "test",
        "_type" : "mydoc",
        "_id" : "YhzDN2kBVzdNcvl_enuT",
        "_score" : 1.0,
        "_source" : {
          "name" : "YYerk",
          "twitter" : "sql is awesome",
          "date" : "2008-12-27",
          "id" : 888888,
          "tags" : [
            "red",
            "green"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "mydoc",
        "_id" : "YxzDN2kBVzdNcvl_u3th",
        "_score" : 1.0,
        "_source" : {
          "name" : "YYerk",
          "twitter" : "sql is awesome",
          "date" : "2008-12-27",
          "id" : 888888,
          "tags" : [
            "red",
            "green"
          ]
        }
      }
    ]
  }
}

如果我想看看id是  "_id" : "YxzDN2kBVzdNcvl_u3th",

这个文档是保存在哪个分片,如何查看?

 

引用:

路由一个文档到一个分片中编辑

当索引一个文档的时候,文档会被存储到一个主分片中。 Elasticsearch 如何知道一个文档应该存放到哪个分片中呢?当我们创建文档时,它如何决定这个文档应当被存储在分片 1 还是分片 2 中呢?

首先这肯定不会是随机的,否则将来要获取文档的时候我们就不知道从何处寻找了。实际上,这个过程是根据下面这个公式决定的:

shard = hash(routing) % number_of_primary_shards

routing 是一个可变值,默认是文档的 _id ,也可以设置成一个自定义的值。 routing 通过 hash 函数生成一个数字,然后这个数字再除以 number_of_primary_shards (主分片的数量)后得到 余数 。这个分布在 0 到 number_of_primary_shards-1 之间的余数,就是我们所寻求的文档所在分片的位置。

这就解释了为什么我们要在创建索引的时候就确定好主分片的数量 并且永远不会改变这个数量:因为如果数量变化了,那么所有之前路由的值都会无效,文档也再也找不到了。

 

那么可以使用

GET test/_search_shards?routing=ID号

 来查看你要查询的id所在的分片

得到的结果:

{
  "nodes" : {
    "yl-qYmh1SXqzJsfI4d1ddw" : {
      "name" : "node-3",
      "ephemeral_id" : "UsJ9rFELTiCW07oHE9YMdg",
      "transport_address" : "10.18.6.26:9300",
      "attributes" : {
        "ml.machine_memory" : "6088101888",
        "rack" : "r1",
        "ml.max_open_jobs" : "20",
        "xpack.installed" : "true",
        "ml.enabled" : "true"
      }
    },
    "wT7wUd3iTkujYUsbVNVv-w" : {
      "name" : "node-1",
      "ephemeral_id" : "fP-vgSb0SdemnHDyaJUsWw",
      "transport_address" : "10.18.6.102:9300",
      "attributes" : {
        "ml.machine_memory" : "8256720896",
        "rack" : "r1",
        "xpack.installed" : "true",
        "ml.max_open_jobs" : "20",
        "ml.enabled" : "true"
      }
    }
  },
  "indices" : {
    "test" : { }
  },
  "shards" : [
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "wT7wUd3iTkujYUsbVNVv-w",
        "relocating_node" : null,
        "shard" : 1,
        "index" : "test",
        "allocation_id" : {
          "id" : "k-8E4dL7QmGgwcsNsUCP6Q"
        }
      },
      {
        "state" : "STARTED",
        "primary" : false,
        "node" : "yl-qYmh1SXqzJsfI4d1ddw",
        "relocating_node" : null,
        "shard" : 1,
        "index" : "test",
        "allocation_id" : {
          "id" : "lvOpQIKgRUibkulr3nRfEw"
        }
      }
    ]
  ]
}

只需要关注shards字段就可以,从上面可以看到,该文档存在shard 1 主分片上。

 

原文链接:

http://30daydo.com/article/493

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页