elasticsearch 动态模板

在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

posted @ 2017-09-12 15:09  海&叶  阅读(7034)  评论(0编辑  收藏  举报