Primary Shard and Replica Shard

2022-12-01 21:31:13 浏览数 (1)

Primary Shard and Replica Shard

Primary Shard

Primary Shard,即主本分片。每个文档都会存储在主本分片中。具体地,当索引一条文档(indexing a document)时,Elasticsearch首先将该文档保存在主本分片中,然后将其拷贝至主本分片的所有副本分片中。默认地,一个索引拥有一个主本分片;在创建索引时,我们可以通过number_of_shards参数指定主本分片的数量,如下:

代码语言:javascript复制
PUT /<index-name>
{
    "settings": {
        "index": {
            "number_of_shards": 3
        }
    }
}

索引一旦生成,那么就不允许修改主本分片数量,因为文档归属于哪一个主本分片是由如下公式计算得出的,如果主本分片数量发生变更,那么就破坏了这种路由规则。

代码语言:javascript复制
shard_num = hash(_routing) % num_primary_shards

Replica Shard

Replica Shard,即副本分片。副本分片是主本分片的拷贝,每个主本分片可以拥有零个或者多个副本分片,但主本分片与其副本分片不会落在同一Elasticsearch节点中。副本分片有以下优点:

  • increase failover, a replica shard can be promoted to a primary shard if the primary fails.
  • increase performance, get and search requests can be handled by primary or replica shards.

默认地,每个主本分片拥有一个副本分片;索引一旦生成,我们依然可以通过number_of_replicas参数动态地修改副本分片数量,如下:

代码语言:javascript复制
PUT /<index-name>/_settings
{
    "settings": {
        "index": {
            "number_of_replicas": 2
        }
    }
}

A shard is a single Lucene instance. It is a low-level “worker” unit which is managed automatically by Elasticsearch.

0 人点赞