软件编程
位置:首页>> 软件编程>> java编程>> 详解Elasticsearch如何把一个索引变为只读

详解Elasticsearch如何把一个索引变为只读

作者:Elasticsearch  发布时间:2023-01-22 12:00:59 

标签:Elasticsearch,索引,只读

将索引设置为只读可能听起来很奇怪,但在 Elasticsearch 中执行此类操作是可能的。想象一下这样一种情况,你特别需要限制对索引的写入操作,无论是维护、业务规则还是任何其他原因。让我们学习如何将索引配置为已读以及如何撤消操作。

我们先使用如下的命令来创建一个叫做 test 的索引:


 PUT test/_doc/1
 {
   "content": "I am xiaoguo from Elastic"
 }

设置为只读

要进行此更改,我们需要更新索引设置。 下面的命令将使索引成为只读的。


 PUT /test/_settings
 {
   "index": {
     "blocks": {
       "write": true
     }
   }
 }

执行完上面的命令后,我们可以再接着创建一个如下的一个文档:


 PUT test/_doc/2
 {
   "content": "I am an evangelist as well"
 }

我们可以看到如下的一个响应:

{
  "error": {
    "root_cause": [
      {
        "type": "cluster_block_exception",
        "reason": "index [test] blocked by: [FORBIDDEN/8/index write (api)];"
      }
    ],
    "type": "cluster_block_exception",
     "reason": "index [test] blocked by: [FORBIDDEN/8/index write (api)];"
   },
   "status": 403
 }

要恢复只需将状态从 true 更改为 false。我们试着运行如下的命令:


 PUT /test/_settings
 {
   "index": {
     "blocks": {
       "write": false
     }
   }
 }

我们再次写入我们想要的文档。我们可以看到这次的写入是成功的:


 PUT test/_doc/2
 {
   "content": "I am an evangelist as well"
 }

上面的响应为:


{
  "_index": "test",
  "_id": "2",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
   },
   "_seq_no": 1,
   "_primary_term": 1
 }

希望这个能帮助到你。

来源:https://juejin.cn/post/7196699593836331068

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com