在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

最新文章

  1. cocos2d-x宏定义
  2. 模仿mybatis,用jdk proxy实现接口
  3. Codeforces 703B (模拟) Mishka and trip
  4. Cordoval在iOS中的运用整理
  5. CentOS下如何完全卸载MySQL?卸载自带的mysql
  6. C语言 字符串操作两头堵模型
  7. Html5编辑工具
  8. Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
  9. 帝国cms实现自动生成缩略图和自动分页功能
  10. linux---finger命令
  11. 跨平台运行ASP.NET Core 1.0(转载)
  12. 解决tomcat运行报错java.lang.UnsatisfiedLinkError: apache-tomcat-7.0.37\bin\tcnative-1.dll:Can load AMD 64
  13. JSTL标签分类
  14. Ubuntu18.04安装Tensorflow
  15. H5微信单页读书日活动
  16. Mysql 根据时间统计总数
  17. 行内元素和块级元素的具体区别是什么?inline-block是什么?(面试题目)
  18. MapReduce与批处理------《Designing Data-Intensive Applications》读书笔记14
  19. Linux下设置和查看环境变量(转)
  20. Accounting_会计电算化工作指南

热门文章

  1. PHP memcache扩展模块安装
  2. NOIP 转圈游戏
  3. jedis客户端,取redis服务的值
  4. AtCoder Regular Contest 095
  5. windchill系统——一些功能查找
  6. Java 正则表达式 Pattern & Matcher
  7. Python基础笔记系列五:元组
  8. C#抽象类和接口
  9. 利用大数据技术处理海量GPS数据
  10. B/S,C/S简单介绍