在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. Spark 实现自定义对象sequenceFile方式存储,读写示例(scala编写)

    package com.fuge.bigdata.datahub.analysis import java.io.{DataInput, DataOutput} import com.fuge.big ...

  2. RN项目中缩进处理

    SpannableString使用详解http://blog.csdn.net/u012702547/article/details/49895157 Spannable的用法http://www.j ...

  3. 1044: [HAOI2008]木棍分割

    1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2161  Solved: 779[Submit][Statu ...

  4. CVE补丁安全漏洞【学习笔记】

    更新安卓系统的CVE补丁网站:https://www.cvedetails.com/vulnerability-list/vendor_id-1224/product_id-19997/version ...

  5. Jenkins自动打包配置

    当时也是花费了不少时间来配置Jenkins自动打包的问题,觉得还是需要记录一下. 1.安装Jenkins,这个很简单,不需要多说. 2.下载Git Plugin,Gradle Plugin,Andro ...

  6. Spring Boot 中使用jsp

    接SpringBoot 快速入门(Eclipse): 步骤一:视图支持 Springboot的默认视图支持是Thymeleaf,但是Thymeleaf我们不熟悉,我们熟悉的还是jsp. 所以下面是讲解 ...

  7. lastIndexOf 方法 (Array) (JavaScript)

    lastIndexOf 方法 (Array) (JavaScript) 返回指定的值在数组中的最后一个匹配项的索引. 语法         array1.lastIndexOf(searchEleme ...

  8. thinkphp Composer安装指南

    1.首先我们去composer的官网下载composer,当然也可以用命令行的形势下下载,我是在windows安装的.https://www.phpcomposer.com/ 2.下载以后进行安装,一 ...

  9. 使用Entity Framework时遇到的各种问题总结

    在这里记录一下之前使用Entity Framework(4.3.1版本)遇到的问题. 更新没有设置主键的表 在默认情况下,EF不能对一个没有主键的表进行更新.插入和删除的动作.用xml方式查看edmx ...

  10. PowerShell 在hyper-v中创建虚拟机

    # This script configures the Hyper-V machines used for the 50331 Course. # PowerShell 3.0 and Window ...