在elasticsearch中,如果你有一类相似的数据字段,想要统一设置其映射,就可以用到一项功能:动态模板映射(dynamic_templates)。

每个模板都有一个名字用于描述这个模板的用途,一个 mapping 字段用于指明这个映射怎么使用,和至少一个参数(例如 match)来定义这个模板适用于哪个字段。
 
参数:
  match_mapping_type允许你只对特定类型的字段使用模板,正如标准动态映射规则那样,比如string,long等。
  match(unmatch相反)参数只会匹配字段名,如"*_es",如果为"*",就是所有字段(同时是match_papping_type类型)都会匹配到
  path_match(path_unmatch相反)参数用于匹配对象中字段的完整路径,比如address.*.name可以匹配如下字段:
    {
    "address":{
    "city":{
    "name": "New York"
     }
    }
    } 下面分两种情况进行举例:
  第一种:直接在普通的mapping中设置
    
curl -XPUT localhost:9200/my_index -d '{     
  "mappings":{         
    "my_type":{               # 文档类型
      "dynamic_templates":  # 关键词,固定的
      [                     # 必须是中括号
         {                    
           "es":{           #模板名                                                  
            "match":"*_es",       #匹配规则                                         
            "match_mapping_type":"string",   #匹配类型                               
            "mapping":{                                                        
              "type":"text",                             # 转换成的类型
              "anaylzer":"spanish"                        
             }                     
            }                
         },                 
        {                     
          "en":{                                                            
            "match":"*",                                                   
            "match_mapping_type":"string",                                
            "mapping":{                                                    
              "type":"text",                             
              "anaylzer":"english"                        
             }                     
          }                 
        },
        {
          "date":{
            "unmatch":"*_es",
            "match_mapping_type":"date",
            "mapping":{
              "type":"keyword"
            }
          }
        }             
      ]        
     }     
  }
}'
添加数据:
  curl -XPOST localhost:9200/my_index/my_type -d '{
    "str_es":"xxx",
    "long_es":124,
    "date_es":"2017-09-12",
    "long_en":123,
    "str_en":"sxx",
    "date_en":"2017-09-12"
  }'
 
查询mapping结果:http://localhost:9200/my_index/_mapping?pretty
{
"my_index" : {
"mappings" : {
"my_type" : {
"dynamic_templates" : [
{
"es" : {
"match" : "*_es",
"match_mapping_type" : "string",
"mapping" : {
"anaylzer" : "spanish",
"type" : "text"
}
}
},
{
"en" : {
"match" : "*",
"match_mapping_type" : "string",
"mapping" : {
"anaylzer" : "english",
"type" : "text"
}
}
},
{
"date" : {
"unmatch" : "*_es",
"match_mapping_type" : "date",
"mapping" : {
"type" : "keyword"
}
}
}
],
"properties" : {
"date_en" : {
"type" : "keyword" #匹配date模板的unmatch:"*_es",date->keyword
},
"date_es" : {
"type" : "date"
},
"long_en" : {
"type" : "long"
},
"long_es" : {
"type" : "long"
},
"str_en" : {
"type" : "text"    #匹配到en模板的"*",string->text
},
"str_es" : {
"type" : "text"    #匹配到es模板的"*_es",string->text
}
}
}
}
}
} 第二种情况,在索引模板中定义动态模板
curl -XPUT localhost:9200/_template/template_1 -d '  
{  
    "template" : "es*",  
  "order":1,
    "settings" : {  
         "number_of_shards" : 2
    },  
    "mappings" : {  
         "_default_" : {  
             "_source" : {"enabled" : true } ,
    "_all":{"enabled":false},
    "properties":{
      "date":{"type":"date"}
    },
     "dynamic_templates":[                 
    {                     
      "int":{                                                             
         "match":"*",                                                
         "match_mapping_type":"long",                               
         "mapping":{                                                        
          "type":"integer"                         
        }                     
      }                  
     }
    ]
  }
 }
}'
 
创建索引
curl -XPUT 'localhost:9200/estest/my_test/1' -d '{
  "age":23,
  "name":"Tom",
  "test_es":234,
  "date":"2017-09-07",
  "text":"The quick & brown fox & &."
}'
 
查看mapping:http://localhost:9200/estest/_mapping?pretty
{
"estest" : {
"mappings" : {
"my_test" : {
"_all" : {
"enabled" : false
},
"dynamic_templates" : [
{
"int" : {
"match" : "*",
"match_mapping_type" : "long",
"mapping" : {
"type" : "integer"
}
}
}
],
"properties" : {
"age" : {
"type" : "integer" #匹配int模板,long->integer
},
"date" : {
"type" : "date"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"test_es" : {
"type" : "integer"  #匹配int模板,long->integer
},
"text" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"_default_" : {
"_all" : {
"enabled" : false
},
"dynamic_templates" : [
{
"int" : {
"match" : "*",
"match_mapping_type" : "long",
"mapping" : {
"type" : "integer"
}
}
}
],
"properties" : {
"date" : {
"type" : "date"
}
}
}
}
}
}
还有不清楚的可以看官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html

elasticsearch 动态模板的更多相关文章

  1. elasticsearch 动态模板设置

    自定义动态映射 如果你想在运行时增加新的字段,你可能会启用动态映射.然而,有时候,动态映射 规则 可能不太智能.幸运的是,我们可以通过设置去自定义这些规则,以便更好的适用于你的数据. 日期检测 当 E ...

  2. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  3. Logstash动态模板映射收集Nginx的Json格式日志

    Logstash传输给ES的数据会自动映射为5索引,5备份,字段都为text的的索引.这样基本上无法进行数据分析.所以必须将Logstash的数据按照既定的格式存储在ES中,这时候就要使用到ES模板技 ...

  4. Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板

    原文:Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...

  5. vert.x学习(六),动态模板与静态文件的结合

    这篇学习在动态模板里面引入css,把动态模板与静态文件结合起来使用. 编写DynamicReference.java package com.javafm.vertx.helloworld; impo ...

  6. 迷你MVVM框架 avalonjs 沉思录 第3节 动态模板

    模板的发明是编程史上的一大里程碑,让我们摆脱了烦锁且易出错的字符串拼接,维护性大大提高. 都在JSP,ASP时代,人们已经学会使用include等语句,将多个页面片断拼接成一个页面. 此外,为了将数据 ...

  7. Elasticsearch索引模板-转载

    转载地址:https://dongbo0737.github.io/2017/06/13/elasticsearch-template/#similar_posts Elasticsearch索引模板 ...

  8. 使用FreeMarker配置动态模板

    FreeMarker动态模板 目录 FreeMarker动态模板 前言 准备工作 FreeMarker 代码构建 项目结构 创建 Configuration 实例 调用 模板文件 调用结果 Tips ...

  9. ElasticStack学习(八):ElasticSearch索引模板与聚合分析初探

    一.Index Template与Dynamic Template的概念 1.Index Template:它是用来根据提前设定的Mappings和Settings,并按照一定的规则,自动匹配到新创建 ...

随机推荐

  1. jQuery可自动播放动画的焦点图

    在线演示 本地下载

  2. Linux 一键安装最新内核并开启 BBR 脚本

    原文链接   https://teddysun.com/489.html 请到原文链接仔细阅读后操作.建议查看过脚本内容后操作,方便理解运行过程. 使用root用户登录,运行以下命令: wget -- ...

  3. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  4. JNI_Z_07_方法的操作(没有String类型的参数)_参数的传递方式

    1. 2.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_TjniMethod02.h" JNIEXPORT void ...

  5. OAuth2疑问解答

    转自:http://bylijinnan.iteye.com/blog/2277548 OAuth2的学习,我也是从阮一峰老师的博客中开始的:http://www.ruanyifeng.com/blo ...

  6. .net操作Oracle数据库步骤及方法

    1.首先安装PL/SQL Developer Oracle客户端软件 2.安装Oracle Instant Client(即时客户端) 安装与配置 配置环境变量ORAClE HOME 地址为insta ...

  7. java中的几种实体类对象(PO,VO,DAO,BO,POJO)

    一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 二.VO :(value object) ...

  8. git回滚到某一个commit

    git reset 046bd7b5c1d134b8123f59ea71b19875a6a2fc3e git reset --hard 046bd7b5c1d134b8123f59ea71b19875 ...

  9. 监控和审计IBM InfoSphere BigInsights和Cloudera Hadoop的访问权限

    http://www.ithov.com/server/124456.shtml 您也将学习一个仅适用于 IBM InfoSphere BigInsights 的快速启动监控实现. 大数据骚动主要集中 ...

  10. opencv:图像的腐蚀和膨胀

    1.图像的腐蚀 图像的腐蚀和膨胀都是相对于像素值高(白色方向)说的,腐蚀简单的说就是白色”被腐蚀“了,也就是像素值低(黑色方向)的变多,白色变少. 腐蚀的原理是利用一个内核对图像进行卷积(扫描),内核 ...