美文网首页
Elasticsearch 7.X : 设置索引副本数量和分片数

Elasticsearch 7.X : 设置索引副本数量和分片数

作者: 觉释 | 来源:发表于2020-08-15 10:04 被阅读0次

    创建名为 test_index的索引 :

    PUT test_index
    

    注意,PUT test_index和 PUT /test_index效果是一样的。
    响应:

    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "test_index"
    }
    

    查询索引信息:

    GET test_index
    

    响应:

    {
      "test_index" : {
        "aliases" : { },
        "mappings" : { },
        "settings" : {
          "index" : {
            "creation_date" : "1597456553836",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "b3GPz-gCRgmdpgcgEELY2A",
            "version" : {
              "created" : "7080099"
            },
            "provided_name" : "test_index"
          }
        }
      }
    }
    

    可以看到,分片数量number_of_shards 为1,副本数量为number_of_replicas 1 。

    修改副本数量

    请求:

    PUT test_index/_settings
    {
      "index" : {
        "number_of_replicas" : 3
      }
    }
    

    响应:

    {
      "acknowledged" : true
    }
    

    注意,当副本创建完成后(可能耗时很长),才会响应。如果耗时很长,可以不用等待响应。过一段时间后去查询索引信息确认即可。

    修改分片数量
    分片数量,必须在索引创建时指定,创建后无法修改。尝试修改时会报错:

    请求

    PUT test_index/_settings
    {
      "index" : {
        "number_of_shards" : 3
      }
    }
    

    响应

    {
     "error" : {
       "root_cause" : [
         {
           "type" : "illegal_argument_exception",
           "reason" : "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[test_index/b3GPz-gCRgmdpgcgEELY2A]]"
         }
       ],
       "type" : "illegal_argument_exception",
       "reason" : "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[test_index/b3GPz-gCRgmdpgcgEELY2A]]"
     },
     "status" : 400
    }
    
    

    创建索引时指定分片数量、副本数量

    删除之前创建的索引:

    DELETE test_index
    

    重新创建 test_index索引:

    PUT test_index
    {
      "settings" : {
        "index" : {
          "number_of_shards" : 3,
          "number_of_replicas" : 1
        }
      }
    }
    

    响应:

    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "test_index"
    }
    

    查看索引信息:

    请求

    GET test_index
    

    响应

    {
     "test_index" : {
       "aliases" : { },
       "mappings" : { },
       "settings" : {
         "index" : {
           "creation_date" : "1597456553836",
           "number_of_shards" : "1",
           "number_of_replicas" : "1",
           "uuid" : "b3GPz-gCRgmdpgcgEELY2A",
           "version" : {
             "created" : "7080099"
           },
           "provided_name" : "test_index"
         }
       }
     }
    }
    
    

    给索引设置别名

    POST _aliases
    {
      "actions": [
        {
          "add": {
            "index": "test_index",
            "alias": "test_index_aliasname"
          }
        }
      ]
    }
    

    相关文章

      网友评论

          本文标题:Elasticsearch 7.X : 设置索引副本数量和分片数

          本文链接:https://www.haomeiwen.com/subject/jhnodktx.html