elasticsearch 動態模板

在elasticsearch中,若是你有一類類似的數據字段,想要統一設置其映射,就能夠用到一項功能:動態模板映射(dynamic_templates)。html

每一個模板都有一個名字用於描述這個模板的用途,一個 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
相關文章
相關標籤/搜索