在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. 20145201《Java程序设计》课程总结

    每周读书笔记链接汇总 第一周读书笔记:http://www.cnblogs.com/20145201lzx/p/5249064.html 第二周读书笔记:http://www.cnblogs.com/ ...

  2. Mybatis导入原生配置文件

    在Spring-Mybatis中导入Mybatis原生配置文件 在sqlSessionFactory Bean中设置设置configLocation属性 <property name=" ...

  3. Execute Disable Bit

    “Execute Disable Bit”是Intel在新一代处理器中引入的一项功能,开启该功能后,可以防止病毒.蠕虫.木马等程序利用溢出.无限扩大等手法去破坏系统内存并取得系统的控制权.其工作原理是 ...

  4. HBase 协处理器编程详解,第二部分:客户端代码编写

    实现 Client 端代码 HBase 提供了客户端 Java 包 org.apache.hadoop.hbase.client.coprocessor.它提供以下三种方法来调用协处理器提供的服务: ...

  5. windchill系统——一些功能查找

    1.创建产品 导航栏的浏览——>最近的产品——>全部查看——>新建产品——>填写“名称”.选择“模板”.一定的“说明”.“专用访问权限”一般选择“否”——>“确定”选项 ...

  6. Mine_hibernate

    1. __z知识点\整理_归纳 ==> "ZC_归纳.txt" 和 "ZC_归纳__12_用Eclipse开发hibernate.txt" 2.

  7. 测绘类SCI

    GeoInformatica(国际地理信息系统计算机科学进展杂志)美国International Journal of Geographical Information Science(国际地理信息科 ...

  8. C++构造函数知识点整理(C++11标准)

    引言 构造函数是c++中的一个比较难的语法知识点.编程实践中,由于在很多情况下可以不显示定义构造函数,或者,虽然定义构造函数的方式并不十分适当,但是程序也能正常运行,故而并不是特别引起开发者的重视. ...

  9. FMDB官方使用文档 G-C-D的使用 提高性能(翻译)

    由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此同时,把SQLite的文档页 加到你的书签中.自动引用计数(APC)还是手动内存管理呢?   两种都行,FMDB会 ...

  10. 【spark】常用转换操作:sortByKey()和sortBy()

    1.sortByKey() 功能: 返回一个根据键排序的RDD 示例 val list = List(("a",3),("b",2),("c" ...