文章十一: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"
            }
          }
        }
      }
    }
  }
}
相关推荐
888CC++8 分钟前
java 并发编程
java·开发语言·python
无风听海26 分钟前
JSON Web Token(JWT)完全指南
java·前端·json
Leon-Ning Liu1 小时前
【真实经验分享】 ORA-600 [qesmaGetTblSeg1]
数据库·oracle
与数据交流的路上1 小时前
MySQL 优化 -- 相关
数据库·mysql
Rooting++1 小时前
为什么mysql的表字段的collation会自动变
数据库·mysql
Wch1G0z8A1 小时前
Google 开源了啥,让 AI Agent 碰数据库不再是定时炸弹
数据库·人工智能·开源
JAVA社区1 小时前
Java高级全套教程(十一)—— Kubernetes 超详细企业级实战详解
java·运维·微服务·容器·面试·kubernetes
暴躁小师兄数据学院1 小时前
【AI大数据工程师特训笔记】第14讲:Linux操作系统与shell脚本
大数据·人工智能·笔记
tedcloud1231 小时前
cc-switch评测:多AI Coding Agent管理工具详解
数据库·人工智能·sql·学习·自动化
土狗TuGou2 小时前
SQL内功笔记 · 第8篇:事务的四大特性与隔离级别
数据库·笔记·后端·sql·mysql·oracle