文章十一:ElasticSearch Dynamic Template详解

Dynamic Template(动态模板)是 Elasticsearch 中用于自动化管理动态新增字段映射的工具:

  • 作用:替代 ES 默认的字段类型猜测规则,按自定义逻辑为 "未显式定义的新字段" 指定映射
  • 核心价值:避免 ES 自动映射产生的不合理类型(比如把数字字符串映射为 text),减少后期重建索引的成本;
  • 生效时机:仅对 "写入时动态新增的字段" 生效,已显式定义的字段不受影响。

基于字段类型

下面的例子中展示了这个功能:

当 Elasticsearch 自动检测字段类型为 long 时(比如写入数值 18 200 这类整数),不会使用默认的 long 类型,而是强制映射为 integer 类型。

复制代码
PUT test_dynamic_template
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 50
  },
  "mappings": {
    "dynamic_templates": [
      {
        "integer_type_mapping_hdk_test_name": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      }
    ]
  }
}

字段名称匹配机制

通过设定名称的匹配机制,解释match,unmatch,match_mapping_type规则实现根据名称的动态模板设置。

复制代码
PUT test_dynamic_template
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 50
  },
  "mappings": {
    "dynamic_templates": [
      {
        "income1": {
          "match": "income1*",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "income2": {
          "match": "income2*",
          "mapping": {
            "type": "long"
          }
        }
      },
      {
        "income3": {
          "match_mapping_type":"string",
          "unmatch": "income*",
          "mapping": {
            "type": "ip"
          }
        }
      }
    ]
  }
}

注意,unmatch不能单独使用,需要和match或者是match_mapping_type结合使用。

字段路径匹配机制

通过path_match字段进行设置,下面的例子展示了我们设定之后, 插入的数据类型判断为keyword类型。

复制代码
PUT test_dynamic_template
{
  "mappings": {
    "dynamic_templates":[
      {
        "path_user_mapping_test":{
          "path_match":"user.*",
          "mapping":{
            "type":"keyword"
          }
        }
      }
      ]
  }
}

PUT test_dynamic_template/_doc/1
{
  "user":{
    "income11111":342,
    "income231":57293845,
    "shijian":"127.0.0.1"
  }
}

GET test_dynamic_template/_mapping

{
  "test_dynamic_template": {
    "mappings": {
      "dynamic_templates": [
        {
          "path_user_mapping_test": {
            "path_match": "user.*",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "user": {
          "properties": {
            "income11111": {
              "type": "keyword"
            },
            "income231": {
              "type": "keyword"
            },
            "shijian": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
相关推荐
天行健,君子而铎16 分钟前
数据分类分级的范式转换:从规则匹配到场景化高准确率一键部署
网络·数据库·安全
别动我齐刘海25 分钟前
C++ 工程化学习
java·linux·前端·c++·学习·机器学习·机器人
牛战士coding31 分钟前
MySQL 读写分离完整链路:主从复制、多数据源与 @DS 路由
数据库·mysql
计算机编程指导师1 小时前
【计算机毕设推荐】基于Hadoop+Spark的公共交通运营数据分析与可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·计算机·数据分析·spark·交通
goehou1 小时前
MCP 查数实战:让 LLM 安全地查数据库(只读账号、SQL 校验、参数漂移)
数据库·ai·llm·教程·mcp·function calling
code2cat2 小时前
Java进阶篇之AtomicLongFieldUpdater:把累计额度留在对象字段中
java·cas·并发编程
WeiXin_DZbishe2 小时前
基于springboot城市公交实时运营与调度系统-计算机毕设 附源码【课程设计】66173
java·vue.js·spring boot·后端·python·php·课程设计
我不会插花弄玉2 小时前
7.函数【由浅入深-MySQL】
android·数据库·mysql
泡茶喝茶写代码2 小时前
A股量化数据工程:从 REST 接口到策略信号(第 18 篇):放量缩量异动扫描
java·python·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口