@@ -72,6 +72,167 @@ Elasticsearch 使用 [_JSON_](http://en.wikipedia.org/wiki/Json) 作为文档的
7272
7373![ ] ( https://raw.githubusercontent.com/dunwu/images/dev/snap/20220108215559.PNG )
7474
75+ ### index template
76+
77+ ** ` index template ` ** (索引模板)帮助用户设定 Mapping 和 Setting,并按照一定的规则,自动匹配到新创建的索引之上。
78+
79+ - 模板仅在一个索引被创建时,才会产生作用。修改模板不会影响已创建的索引。
80+ - 你可以设定多个索引模板,这些设置会被 merge 在一起。
81+ - 你可以指定 order 的数值,控制 merge 的过程。
82+
83+ 当新建一个索引时
84+
85+ - 应用 ES 默认的 Mapping 和 Setting
86+ - 应用 order 数值低的 index template 中的设定
87+ - 应用 order 数值高的 index template 中的设定,之前的设定会被覆盖
88+ - 应用创建索引是,用户所指定的 Mapping 和 Setting,并覆盖之前模板中的设定。
89+
90+ 示例:创建默认索引模板
91+
92+ ``` bash
93+ PUT _template/template_default
94+ {
95+ " index_patterns" : [" *" ],
96+ " order" : 0,
97+ " version" : 1,
98+ " settings" : {
99+ " number_of_shards" : 1,
100+ " number_of_replicas" : 1
101+ }
102+ }
103+
104+ PUT /_template/template_test
105+ {
106+ " index_patterns" : [" test*" ],
107+ " order" : 1,
108+ " settings" : {
109+ " number_of_shards" : 1,
110+ " number_of_replicas" : 2
111+ },
112+ " mappings" : {
113+ " date_detection" : false,
114+ " numeric_detection" : true
115+ }
116+ }
117+
118+ # 查看索引模板
119+ GET /_template/template_default
120+ GET /_template/temp*
121+
122+ # 写入新的数据,index以test开头
123+ PUT testtemplate/_doc/1
124+ {
125+ " someNumber" : " 1" ,
126+ " someDate" : " 2019/01/01"
127+ }
128+ GET testtemplate/_mapping
129+ GET testtemplate/_settings
130+
131+ PUT testmy
132+ {
133+ " settings" :{
134+ " number_of_replicas" :5
135+ }
136+ }
137+
138+ PUT testmy/_doc/1
139+ {
140+ " key" : " value"
141+ }
142+
143+ GET testmy/_settings
144+ DELETE testmy
145+ DELETE /_template/template_default
146+ DELETE /_template/template_test
147+ ```
148+
149+ ### dynamic template
150+
151+ - 根据 ES 识别的数据类型,结合字段名称,来动态设定字段类型
152+ - 所有的字符串类型都设定成 Keyword,或者关闭 keyword 字段。
153+ - is 开头的字段都设置成 boolean
154+ - long_ 开头的都设置成 long 类型
155+ - dynamic template 是定义在某个索引的 Mapping 中
156+ - template 有一个名称
157+ - 匹配规则是一个数组
158+ - 为匹配到字段设置 Mapping
159+
160+ 示例:
161+
162+ ``` bash
163+ # Dynaminc Mapping 根据类型和字段名
164+ DELETE my_index
165+
166+ PUT my_index/_doc/1
167+ {
168+ " firstName" : " Ruan" ,
169+ " isVIP" : " true"
170+ }
171+
172+ GET my_index/_mapping
173+
174+ DELETE my_index
175+ PUT my_index
176+ {
177+ " mappings" : {
178+ " dynamic_templates" : [
179+ {
180+ " strings_as_boolean" : {
181+ " match_mapping_type" : " string" ,
182+ " match" : " is*" ,
183+ " mapping" : {
184+ " type" : " boolean"
185+ }
186+ }
187+ },
188+ {
189+ " strings_as_keywords" : {
190+ " match_mapping_type" : " string" ,
191+ " mapping" : {
192+ " type" : " keyword"
193+ }
194+ }
195+ }
196+ ]
197+ }
198+ }
199+ GET my_index/_mapping
200+
201+ DELETE my_index
202+ # 结合路径
203+ PUT my_index
204+ {
205+ " mappings" : {
206+ " dynamic_templates" : [
207+ {
208+ " full_name" : {
209+ " path_match" : " name.*" ,
210+ " path_unmatch" : " *.middle" ,
211+ " mapping" : {
212+ " type" : " text" ,
213+ " copy_to" : " full_name"
214+ }
215+ }
216+ }
217+ ]
218+ }
219+ }
220+ GET my_index/_mapping
221+
222+
223+ PUT my_index/_doc/1
224+ {
225+ " name" : {
226+ " first" : " John" ,
227+ " middle" : " Winston" ,
228+ " last" : " Lennon"
229+ }
230+ }
231+
232+ GET my_index/_search? q=full_name:John
233+ DELETE my_index
234+ ```
235+
75236## Mapping
76237
77238在 Elasticsearch 中,** ` Mapping ` ** (映射),用来定义一个文档以及其所包含的字段如何被存储和索引,可以在映射中事先定义字段的数据类型、字段的权重、分词器等属性,就如同在关系型数据库中创建数据表时会设置字段的类型。
0 commit comments