在elasticsearch中,若是你有一類類似的數據字段,想要統一設置其映射,就能夠用到一項功能:動態模板映射(dynamic_templates
)。html
{
"address":{ "city":{ "name": "New York" } } }
下面分兩種狀況進行舉例:
第一種:直接在普通的mapping中設置
添加數據:
{ "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 } } } } } }
第二種狀況,在索引模板中定義動態模板
{ "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