在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