以编程方式设置索引的read_only_allow_delete属性

时间:2019-03-22 11:00:36

标签: elasticsearch elasticsearch-dsl elasticsearch-dsl-py

我正在尝试执行以下行,但是会引发错误(我应该通过运行相同的代码来避免此错误):

   es.indices.put_settings(index="demo_index", body={
        "blocks": {
            "read_only_allow_delete": "false"
        }
    })

错误:elasticsearch.exceptions.AuthorizationException:AuthorizationException(403,“ cluster_block_exception”,“被以下对象阻止:[FORBIDDEN / 12 / index只读/允许删除(api)];')

我通过使用curl触发了相同的查询,它成功执行,并且没有错误:

curl -XPUT 'localhost:9200/demo_index/_settings' -H 'Content-Type: application/json' -d '{ "index": { "blocks": { "read_only_allow_delete": "false" } } }'

我也尝试使用“ null”代替“ false”,但是得到的结果相同。有想法吗?

2 个答案:

答案 0 :(得分:1)

我没有足够的声誉来添加评论,但是您是否尝试过将body参数与index进行包装以匹配curl命令?

es.indices.put_settings(index="demo_index", body={
        "index": {
            "blocks": {
                "read_only_allow_delete": "false"
            }
        }
    })

答案 1 :(得分:0)

使用新的API,您可以通过以下方式实现此目标:

import elasticsearch

def connect_elasticsearch():
    _es = None
    _es = elasticsearch.Elasticsearch([{'host': 'localhost', 'port': 9200}])
    if _es.ping():
        print('Yay Connect')
    else:
        print('Awww it could not connect!')
    return _es

es = connect_elasticsearch()

try:
    body = {"index.blocks.read_only_allow_delete": "false"}
    es_index_settings = es.indices.put_settings(index="demo_index",body=body)
except elasticsearch.ElasticsearchException as exp:
    print(exp)