Elasticsearch——禁止Body中的index覆盖Url中的index参数

时间:2021-08-20 19:56:13

本篇继续一下Elasticsearch日常使用的技巧翻译。

在Elasticsearch有很多的api支持在body中指定_index等信息,比如mget或者msearch以及bulk。

默认的情况下,body中的index会覆盖掉url中的index参数。比如:

$ curl localhost:9200/test/_mget?pretty -d '{"docs":[{"_index":"test1","_id":1},{"_index":"test2","_id":2}]}'
{
"docs" : [ {
"_index" : "test1",
"_type" : "tet",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source":{"name":"1"}
}, {
"_index" : "test2",
"_type" : null,
"_id" : "2",
"found" : false
} ]
}

虽说在url中指定了index为test,但是执行到每个文档时,仍然会按照body里面的内容为准。

此时可以通过设置参数rest.action.multi.allow_explicit_indexfalse来关闭覆盖功能。

这个设置会对所有的节点起作用,设置方法如下:

config/elasticsearch.yml中添加:

rest.action.multi.allow_explicit_index: false

然后重启Elasticsearch,再次执行就会发现,服务器已经提示拒绝。

$ curl localhost:9200/test/_mget?pretty -d '{"docs":[{"_index":"test1","_id":1},{"_index":"test2","_id":2}]}'
{
"error" : "ElasticsearchIllegalArgumentException[explicit index in multi get is not allowed]",
"status" : 400
}