索引可使用预定义的模板进行创建,这个模板称作Index templates。模板设置包括settings和mappings,通过模式匹配的方式使得多个索引重用一个模板,例如:
定义模板:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1curl -XPUT localhost:9200/_template/template_1 -d '
2{
3 "template" : "te*",
4 "settings" : {
5 "number_of_shards" : 1
6 },
7 "mappings" : {
8 "type1" : {
9 "_source" : {"enabled" : false }
10 }
11 }
12}
13'
14
上述定义的模板
template_1将对用te开头的新索引都是有效。
模板中也可以包含别别名的定义,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 1curl -XPUT localhost:9200/_template/template_1 -d '
2{
3 "template" : "te*",
4 "settings" : {
5 "number_of_shards" : 1
6 },
7 "aliases" : {
8 "alias1" : {},
9 "alias2" : {
10 "filter" : {
11 "term" :{"user" : "kimchy" }
12 },
13 "routing" :"kimchy"
14 },
15 "{index}-alias" : {}
16 }
17}
18
删除模板:
使用模板名称对模板进行删除.
1
2 1curl -XDELETE localhost:9200/_template/template_1
2
同样也可以查看定义的模板:
1
2 1curl -XGET localhost:9200/_template/template_1
2
多个索引模板:
** **当存在多个索引模板时并且某个索引两者都匹配时,settings和mpapings将合成一个配置应用在这个索引上。合并的顺序可由索引模板的order属性来控制。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1curl -XPUT localhost:9200/_template/template_1 -d '
2{
3 "template" : "*",
4 "order" : 0,
5 "settings" : {
6 "number_of_shards" : 1
7 },
8 "mappings" : {
9 "type1" : {
10 "_source" : {"enabled" : false }
11 }
12 }
13}
14'
15==================================================================
16curl -XPUT localhost:9200/_template/template_2 -d '
17{
18 "template" : "te*",
19 "order" : 1,
20 "settings" : {
21 "number_of_shards" : 1
22 },
23 "mappings" : {
24 "type1" : {
25 "_source" : {"enabled" : true }
26 }
27 }
28}
29'
30
上述order为1的配置将覆盖order为0的配置,最终索引的配置
source
的
enabled为true。****
模板配置文件:
除了以上方式,索引模板也可以在文件中进行配置。索引模板的配置文件需要在每个
主节点的config目录下,目录结构为:
config/templates/template_1.json,temp
late_1.json的样例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 1{
2 "template-logstash" : {
3 "template" : "logstash*",
4 "settings" : {
5 "index.number_of_shards" : 5,
6 "number_of_replicas" : 1,
7 "index" : {
8 "store" : {
9 "compress" : {
10 "stored" : true,
11 "tv": true
12 }
13 }
14 }
15 },
16 "mappings" : {
17 "_default_" : {
18 "properties" : {
19 "dynamic" : "true",
20 },
21 },
22 "loadbalancer" : {
23 "_source" : {
24 "compress" : true,
25 },
26 "_ttl" : {
27 "enabled" : true,
28 "default" : "10d"
29 },
30 "_all" : {
31 "enabled" : false
32 },
33 "properties" : {
34 "@fields" : {
35 "dynamic" : "true",
36 "properties" : {
37 "client" : {
38 "type" : "string",
39 "index" : "not_analyzed"
40 },
41 "domain" : {
42 "type" : "string",
43 "index" : "not_analyzed"
44 },
45 "oh" : {
46 "type" : "string",
47 "index" : "not_analyzed"
48 },
49 "responsetime" : {
50 "type" : "double",
51 },
52 "size" : {
53 "type" : "long",
54 "index" : "not_analyzed"
55 },
56 "status" : {
57 "type" : "string",
58 "index" : "not_analyzed"
59 },
60 "upstreamtime" : {
61 "type" : "double",
62 },
63 "url" : {
64 "type" : "string",
65 "index" : "not_analyzed"
66 }
67 }
68 },
69 "@source" : {
70 "type" : "string",
71 "index" : "not_analyzed"
72 },
73 "@timestamp" : {
74 "type" : "date",
75 "format" : "dateOptionalTime"
76 },
77 "@type" : {
78 "type" : "string",
79 "index" : "not_analyzed",
80 "store" : "no"
81 }
82 }
83 }
84 }
85 }
86}
87
【参考】:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html