有没有办法在我的索引中阻止新类型的实体

时间:2015-09-29 14:26:16

标签: indexing elasticsearch

我已经定义了

dynamic "false"

在我的_default映射标记中 这限制了新字段的创建 有没有办法限制要创建的新文档类型(实体类型)? 谢谢。

1 个答案:

答案 0 :(得分:0)

是的,使用_default_映射设置:

PUT /test
{
  "mappings": {
    "_default_": {
      "dynamic": "strict"
    },
    "test": {
.....

使用1.7.1完成测试:

DELETE test
PUT /test
{
  "mappings": {
    "_default_": {
      "dynamic": "strict"
    },
    "test": {
      "dynamic": "strict",
      "properties": {
        "field1": {
          "type": "string"
        },
        "field2": {
          "type": "string"
        },
        "field3": {
          "type": "string"
        }
      }
    }
  }
}

成功测试:

POST /test/test/1
{"field1":"abc","field2":"abc","field3":"abc"}

测试失败(向已创建的类型添加新字段):

POST /test/test/2
{"field1":"abc","field4":"abc"}

{
   "error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [field4] within [test] is not allowed]",
   "status": 400
}

测试失败(创建新类型):

POST /test/test2/3
{"whatever":"some useless text"}

{
   "error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [whatever] within [test2] is not allowed]",
   "status": 400
}