1. 程式人生 > >Elasticsearch——禁止Body中的index覆蓋Url中的index引數

Elasticsearch——禁止Body中的index覆蓋Url中的index引數

本篇繼續一下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
}