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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
[[indices-templates]]
== Index Templates
Index templates allow to define templates that will automatically be
applied to new indices created. The templates include both settings and
mappings, and a simple pattern template that controls if the template
will be applied to the index created. For example:
[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
"template" : "te*",
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : false }
}
}
}
'
--------------------------------------------------
Defines a template named template_1, with a template pattern of `te*`.
The settings and mappings will be applied to any index name that matches
the `te*` template.
[float]
[[delete]]
=== Deleting a Template
Index templates are identified by a name (in the above case
`template_1`) and can be delete as well:
[source,js]
--------------------------------------------------
curl -XDELETE localhost:9200/_template/template_1
--------------------------------------------------
[float]
[[getting]]
=== GETting templates
Index templates are identified by a name (in the above case
`template_1`) and can be retrieved using the following:
[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/template_1
--------------------------------------------------
You can also match several templates by using wildcards like:
[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/temp*
curl -XGET localhost:9200/_template/template_1,template_2
--------------------------------------------------
To get list of all index templates you can use
<<cluster-state,Cluster State>> API
and check for the metadata/templates section of the response.
Or run:
[source,js]
--------------------------------------------------
curl -XGET localhost:9200/_template/
--------------------------------------------------
[float]
[[multiple-templates]]
=== Multiple Template Matching
Multiple index templates can potentially match an index, in this case,
both the settings and mappings are merged into the final configuration
of the index. The order of the merging can be controlled using the
`order` parameter, with lower order being applied first, and higher
orders overriding them. For example:
[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
"template" : "*",
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : false }
}
}
}
'
curl -XPUT localhost:9200/_template/template_2 -d '
{
"template" : "te*",
"order" : 1,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : true }
}
}
}
'
--------------------------------------------------
The above will disable storing the `_source` on all `type1` types, but
for indices of that start with `te*`, source will still be enabled.
Note, for mappings, the merging is "deep", meaning that specific
object/property based mappings can easily be added/overridden on higher
order templates, with lower order templates providing the basis.
[float]
[[config]]
=== Config
Index templates can also be placed within the config location
(`path.conf`) under the `templates` directory (note, make sure to place
them on all master eligible nodes). For example, a file called
`template_1.json` can be placed under `config/templates` and it will be
added if it matches an index. Here is a sample of the mentioned file:
[source,js]
--------------------------------------------------
{
"template_1" : {
"template" : "*",
"settings" : {
"index.number_of_shards" : 2
},
"mappings" : {
"_default_" : {
"_source" : {
"enabled" : false
}
},
"type1" : {
"_all" : {
"enabled" : false
}
}
}
}
}
--------------------------------------------------
|