You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenSearch strict_allow_templates下字典对象的动态模板正确配置方式

解决strict_allow_templates模式下动态字典字段的映射问题

你的错误根源在于:dict这个父对象没有在映射中预先声明。即使你写了针对dict.*子字段的动态模板,在strict_allow_templates模式下,Elasticsearch会把未声明的dict字段视为动态引入的未知字段,直接阻止。

解决方法很简单:在索引映射的properties里显式声明dict为object类型,不需要定义它的子字段——动态模板会自动处理符合规则的子键值对。

正确的索引创建示例

PUT /your_index_name
{
  "settings": {
    "index.mapping.strict_allow_templates": true
  },
  "mappings": {
    "properties": {
      // 显式声明dict为object类型,告诉ES这是合法的父对象
      "dict": {
        "type": "object"
      }
    },
    "dynamic_templates": [
      {
        "dict_numeric_keys": {
          "match_mapping_type": "string",
          "path_match": "dict.\\d{1,19}$",
          "mapping": {
            "type": "text"
          }
        }
      }
    ]
  }
}

为什么这样可行?

  • 显式声明dict为object后,ES不会再把它当成动态引入的未知字段,绕过了strict_allow_templates的限制。
  • 动态模板dict_numeric_keys会匹配dict下所有符合\d{1,19}(1到19位数字)命名规则的子字段,自动将它们映射为text类型。
  • 你完全不用担心文档格式错误:dict可以包含任意数量符合规则的键值对,不符合命名规则的子字段会被strict_allow_templates阻止(这正是你想要的严格映射效果)。

测试验证

索引你提供的示例文档:

POST /your_index_name/_doc
{
  "dict": {
    "146": "lorem ipsum dolum",
    "23297030": "",
    "20454962": "ipsum dolor sit amet"
  }
}

此时不会再触发strict_dynamic_mapping_exception,且所有数字键的子字段都会被正确映射为text类型。

内容的提问来源于stack exchange,提问作者Arthur Caccavo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:27:13