1.设置数据库副本数
--必须是针对已有的数据库,对于还没有创建的数据库,则会报错:
--"reason" : "no such index [myindex2]",
--创建数据库,默认是1个副本。
put myindex2
--修改数据库的副本数为2;
PUT /myindex2/_settings
{
"settings": {
"number_of_replicas": 2
}
}
2.设置刷新频率
--默认的刷新频率是:1s一次。
"refresh_interval": "1s"
--修改刷新频率未知:修改后其实还是1s;
PUT /myindex2/_settings
{
"index" : {
"refresh_interval" : null
}
}
--修改刷新频率为2s;
PUT /myindex2/_settings
{
"index" : {
"refresh_interval" : "2s"
}
}
--已经变为2s;
--已经变为2s;
"refresh_interval": "2s",
3.设置数据库的读写模式。
--(1)修改数据库为只读模式。只可以读取,不可以写入。
PUT /myindex2/_settings
{
"index.blocks.read_only" : true,
"index.blocks.read":false
}
--查看设置,果然变成了只读。
"blocks": {
"read_only": "true"
}
--插入数据试试看。
POST /myindex2/_doc
{
"id":1,
"名称":"雪霜期"
}
--输出如下:
--数据库被只读API阻塞。
{
"error" : {
"root_cause" : [
{
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/5/index read-only (api)];"
}
],
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/5/index read-only (api)];"
},
"status" : 403
}
--读取数据
get /myindex2/_search
--(2)设置数据库不可读取,但是可以写入。
PUT /myindex2/_settings
{
"index.blocks.read_only" : false,
"index.blocks.read":true
}
--果然可以写入,但是不能读取。
get /myindex2/_search
{
"error" : {
"root_cause" : [
{
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/7/index read (api)];"
}
],
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/7/index read (api)];"
},
"status" : 403
}
(3)设置数据库只可读取,不可写入。
PUT /myindex2/_settings
{
"index.blocks.read_only" : false,
"index.blocks.read":false,
"index.blocks.write":true
}
--可以正常读取数据
get /myindex2/_search
--不可以写入。
POST /myindex2/_doc
{
"id":2,
"名称":"xueshuangqi"
}
{
"error" : {
"root_cause" : [
{
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/8/index write (api)];"
}
],
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/8/index write (api)];"
},
"status" : 403
}
(4)设置数据库可以读取和删除,但是不允许写入。
PUT /myindex2/_settings
{
"index.blocks.read_only" : false,
"index.blocks.read":false,
"index.blocks.write":false,
"index.blocks.read_only_allow_delete":true
}
--可以正常读取数据。
get /myindex2/_search
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "Yj56XZABmMobXH8ANpmt",
"_score" : 1.0,
"_source" : {
"id" : 1,
"名称" : "雪霜期"
}
},
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "Zj6CXZABmMobXH8AkZlB",
"_score" : 1.0,
"_source" : {
"id" : 2,
"名称" : "xueshuangqi"
}
}
]
}
}
--尝试写入数据
--可以正常写入。
POST /myindex2/_doc
{
"id":3,
"名称":"huaidan"
}
--删除数据,可以正常删除。
POST /myindex2/_delete_by_query
{
"query": {
"match": {
"名称" : "huaidan"
}
}
}
--由此可见只设置:index.blocks.read_only_allow_delete=true,
既不会阻塞读,也不会阻塞写。
应该是:index.blocks.read_only_allow_delete=true,
index.blocks.read_only=true,需要同时启用才行。
PUT /myindex2/_settings
{
"index.blocks.read_only" : true,
"index.blocks.read":false,
"index.blocks.write":false,
"index.blocks.read_only_allow_delete":true
}
--插入数据。不可以插入:即阻塞了write写入。
POST /myindex2/_doc
{
"id":3,
"名称":"huaidan"
}
{
"error" : {
"root_cause" : [
{
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/5/index read-only (api)];"
}
],
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/5/index read-only (api)];"
},
"status" : 403
}
--读取数据,可以正常读取。
get myindex2/_search
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "Yj56XZABmMobXH8ANpmt",
"_score" : 1.0,
"_source" : {
"id" : 1,
"名称" : "雪霜期"
}
},
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "Zj6CXZABmMobXH8AkZlB",
"_score" : 1.0,
"_source" : {
"id" : 2,
"名称" : "xueshuangqi"
}
}
]
}
}
--删除:可以正常删除。
POST /myindex2/_delete_by_query
{
"query": {
"match": {
"名称" : "xueshuangqi"
}
}
}
(5)阻塞元数据的操作
PUT /myindex2/_settings
{
"index.blocks.read_only" : false,
"index.blocks.read":false,
"index.blocks.write":false,
"index.blocks.read_only_allow_delete":false,
"index.blocks.metadata":true
}
--添加字段,不允许。
POST myindex2/_update/1
{
"script" : "ctx._source.age = 18"
}
--报错如下:
{
"error" : {
"root_cause" : [
{
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/9/index metadata (api)];"
}
],
"type" : "cluster_block_exception",
"reason" : "index [myindex2] blocked by: [FORBIDDEN/9/index metadata (api)];"
},
"status" : 403
}
--删除字段 ,将文档_id=1的记录的"名称"列删除。
POST myindex2/_update/1
{
"script" : "ctx._source.remove('名称')"
}
--现在取消元数据的阻塞,重新增加和删除字段。
PUT /myindex2/_settings
{
"index.blocks.read_only" : false,
"index.blocks.read":false,
"index.blocks.write":false,
"index.blocks.read_only_allow_delete":false,
"index.blocks.metadata":false
}
--添加字段,ID=1的文档。
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1
}
}
POST myindex2/_update/1
{
"script" : "ctx._source.age = 18"
}
--查看更新后,文档的结构。、
get myindex2/_search
--回显内容包含如下:
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"age" : 18
}
--删除:文档ID=1,的文档中age字段。
POST myindex2/_update/1
{
"script" : "ctx._source.remove('age')"
}
--删除后文档变成如下:
{
"_index" : "myindex2",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1
}
4.总结
index.blocks.read_only:设为 true,则索引以及索引的元数据只可读。
index.blocks.read_only_allow_delete:设为 true,
同时:index.blocks.read_only:设为 true,允许只读和删除,但不能写入。
index.blocks.read:设为 true,则不可读。阻塞读,但是可以写入。
index.blocks.write:设为 true,则不可写。阻塞写,但是可以读取。
index.blocks.metadata:设为 true,则索引元数据不可读写。
阻塞元数据,不可以修改已经存在数据的文档的结构。