【问题标题】:Disable dynamic mapping completely in Elasticsearch在 Elasticsearch 中完全禁用动态映射
【发布时间】:2021-03-25 10:31:49
【问题描述】:

我有一个索引模板,我正在创建一个索引

PUT /_index_template/example_template
{
   "index_patterns": [
        "example*"
    ],
    "priority": 1,
    "template": {
        "aliases": {
            "example":{}
        },
    "mappings": {
"dynamic":strict,
      "_source":
      {"enabled": false},
      "properties": {
       "SomeID":
        { "type": "keyword", "index" : true,"store":true,"ignore_above":5},
       "firstName":
        { "type": "text", "index" : true,"store":true},
        "lastName":
        { "type": "text", "index" : false},
        "PersonInfo": {
        "type": "object",
"dynamic":"true",
        "properties": {
          "FirstName": {
            "type": "keyword",
            "index": true,
            "store": false
          }
        }
        }
      }
    },
    "settings": {
    "index": {
      "number_of_shards": 1,  
      "number_of_replicas": 3
    }
  }
  }
}

在模板映射中,您可以看到我将动态设置为 Strict,因此无法将新字段添加到映射中, 在内部对象 PersonInfo 上,我可以将 dynamic 设置为 true,这具有优先权并允许插入新的字段映射。

PUT example10022021/_doc/1
{
   "SomeID":"1234",
   "firstName":"Nishikant",
   "PersonInfo.service_data":"random"
}

这里 service_data 被添加到映射中,因为动态是真的

"PersonInfo" : {
          "dynamic" : "true",
          "properties" : {
            "FirstName" : {
              "type" : "keyword"
            },
            "service_data" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }

有没有办法完全禁用动态映射?喜欢全局指定?

谢谢!

@Val 回答后我采取的步骤:

PUT /_index_template/example_template
{
   "index_patterns": [
        "example*"
    ],
    "priority": 1,
    "template": {
        "aliases": {
            "order":{}
        },
    "mappings": {
      "dynamic": "strict",
      "dynamic_templates": [
        {
          "objects": {
            "match_mapping_type": "object",
            "mapping": {
              "dynamic": "strict"
            }
          }
        }
      ],
      "_source":
      {"enabled": false},
      "properties": {
       "BillToID":
        { "type": "keyword", "index" : true,"store":true,"ignore_above":5},
       "firstName":
        { "type": "text", "index" : true,"store":true},
        "lastName":
        { "type": "text", "index" : false},
        "PersonInfo": {
        "type": "object",
        "dynamic":true,
        "properties": {
          "FirstName": {
            "type": "keyword",
            "index": true,
            "store": false
          }
        }
        }
      }
    },
    "settings": {
    "index": {
      "number_of_shards": 1,  
      "number_of_replicas": 3
    }
  }
  }
}

然后我创建一个索引

PUT example10022021

然后插入一个文档

POST example10022021/_doc/1
{
   "BillToID":"1234",
   "firstName":"Nishikant",
   "PersonInfo.service_data":"random"
}

这将导致 200OK,现在如果您再次检查映射

GET example10022021

在 o/p 中,您可以看到添加了动态字段映射(我不希望这种行为),

"PersonInfo" : {
          "dynamic" : "true",
          "properties" : {
            "FirstName" : {
              "type" : "keyword"
            },
            "service_data" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    您可以做的是创建另一个适用于所有索引的索引模板,即使用* 名称模式:

    PUT /_index_template/common_template
    {
       "index_patterns": [
            "*"
        ],
        "priority": 0,
        "template": {
           "mappings": {
             "dynamic": "strict",
             ...
    

    如果您还想限制在内部对象中创建动态字段,您可以利用dynamic templates,如下所示:

    PUT /_index_template/common_template
    {
      "index_patterns": [
        "*"
      ],
      "priority": 1000,
      "template": {
        "settings": {},
        "mappings": {
          "dynamic": "strict",
          "dynamic_templates": [
            {
              "objects": {
                "match_mapping_type": "object",
                "mapping": {
                  "dynamic": "strict"
                }
              }
            }
          ],
          "properties": {
            "test": {
              "type": "object",
              "properties": {
                "inner": {
                  "type": "integer"
                }
              }
            }
          }
        }
      }
    }
    

    使用上面的索引模板,你可以创建一个像这样的文档:

    POST test/_doc/
    {
      "test": {
        "inner": 1
      }
    }
    

    但不是这样的:

    POST test/_doc/
    {
      "test": {
        "inner": 1,
        "inner2": 2        <--- this will not be allowed
      }
    }
    

    【讨论】:

    • 感谢您与我们联系!我按照您所说的@Val 进行了尝试,但我仍然能够添加一个带有字段的新文档,该字段最初不存在于我的索引模板映射中,并且这些字段映射正在被添加,因为我已将动态设置为 true,在内部对象级别,但我不希望这种行为,我希望将动态严格应用于所有对象,或者像索引级别动态映射一样具有最高优先级,而不是内部对象
    • 我使用的是 ES 7.10,我已经更新了问题的更多细节
    • 我跟进你的 API,它工作,但是当我提供 "test": { "type": "object","dynamic": true,..这里这个内部级别的动态是优先考虑,并在映射中添加一个新字段
    • 当然,直接用PUT index创建索引时,如果你提供了mappings部分,它总是优先于模板
    • 不,我不是使用 PUT 索引创建新索引,我只是创建索引模板,使用您提供的动态模板,只是在测试对象上添加 dynamic=true 然后进行 POST 测试/_doc /1{"test":"inner": 1, "inner2": 2 }..这里 inner2 被添加为字段映射,因此它不遵循来自动态模板的严格,而是指定为 true 的动态。跨度>
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多