建立與索引名關聯的templates,寫入的時候指定索引名,根據es自動建立索引的特性+templates自動建立本身須要的索引html
Dynamic templates 用於自定義在動態添加field的時候自動給field設置的數據類型, 給什麼類型基於:java
Dynamic templates are specified as an array of named objects:正則表達式
"dynamic_templates": [ { "my_template_name": { ... match conditions ... "mapping": { ... } } }, ... ]
my_template_name
can be any string value- The
match conditions
can include any of : match_mapping_type, match, match_pattern, unmatch, path_match, path_unmatch.- The mapping that the matched field should use
Templates將會順序執行,直到新增的字段value某一個templates的match condition。能夠經過
PUT mapping
API向templates list中追加templates,若是新增的templates和某個舊的重名,它將會替代那個舊的。api
match_mapping_type用於匹配被dynamic field mapping識別到的es數據類型,換句話說,是es對這個field應該是什麼類型的猜想。只有如下數據類型能被es自動識別:app
boolean, date, double, long, object, string
能夠在定義match_mapping_type的時候用*來表示匹配全部數據類型。
示例:elasticsearch
把自動識別爲long類型的field定義爲interger類型,把自動識別爲string類型的field同時定義爲analyzed和not_analyzed: PUT my_index { "mappings": { "my_type": { "dynamic_templates": [ { "integers": { "match_mapping_type": "long", "mapping": { "type": "integer" } } }, { "strings": { "match_mapping_type": "string", "mapping": { "type": "string", "fields": { "raw": { "type": "string", "index": "not_analyzed", "ignore_above": 256 } } } } } ] } } }
match和unmatch定義應用於filedname的pattern。
示例:ide
定義一個匹配全部以long_開頭且不以_text結束的string類型的模板 PUT my_index { "mappings": { "my_type": { "dynamic_templates": [ { "longs_as_strings": { "match_mapping_type": "string", "match": "long_*", "unmatch": "*_text", "mapping": { "type": "long" } } } ] } } }
用於調整match
參數的行爲,好比當match_pattern的值爲regex時,match能夠支持完整的java正則表達式而不是簡單的通配符。ui
用於object類型code
{name}和{dynamic_type}佔位符放在mapping中,值爲field name和field被es識別的數據類型orm
能夠經過設置_default_類型的mapping覆蓋默認的template,影響範圍爲全部的indices和types
示例以下
(5.x+): PUT _template/template_1 { "index_patterns": ["te*", "bar*"], "settings": { "number_of_shards": 1 }, "mappings": { "_doc": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "keyword" }, "created_at": { "type": "date", "format": "EEE MMM dd HH:mm:ss Z YYYY" } } } } }
(2.3) PUT /_template/template_1 { "template": "te*", "settings": { "number_of_shards": 1 }, "mappings": { "type1": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "string", "index": "not_analyzed" }, "created_at": { "type": "date", "format": "EEE MMM dd HH:mm:ss Z YYYY" } } } } }