templates.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. [[dynamic-templates]]
  2. === Dynamic templates
  3. Dynamic templates allow you to define custom mappings that can be applied to
  4. dynamically added fields based on:
  5. * the <<dynamic-mapping,datatype>> detected by Elasticsearch, with <<match-mapping-type,`match_mapping_type`>>.
  6. * the name of the field, with <<match-unmatch,`match` and `unmatch`>> or <<match-pattern,`match_pattern`>>.
  7. * the full dotted path to the field, with <<path-match-unmatch,`path_match` and `path_unmatch`>>.
  8. The original field name `{name}` and the detected datatype
  9. `{dynamic_type`} <<template-variables,template variables>> can be used in
  10. the mapping specification as placeholders.
  11. IMPORTANT: Dynamic field mappings are only added when a field contains a
  12. concrete value -- not `null` or an empty array. This means that if the
  13. `null_value` option is used in a `dynamic_template`, it will only be applied
  14. after the first document with a concrete value for the field has been
  15. indexed.
  16. Dynamic templates are specified as an array of named objects:
  17. [source,js]
  18. --------------------------------------------------
  19. "dynamic_templates": [
  20. {
  21. "my_template_name": { <1>
  22. ... match conditions ... <2>
  23. "mapping": { ... } <3>
  24. }
  25. },
  26. ...
  27. ]
  28. --------------------------------------------------
  29. <1> The template name can be any string value.
  30. <2> The match conditions can include any of : `match_mapping_type`, `match`, `match_pattern`, `unmatch`, `path_match`, `path_unmatch`.
  31. <3> The mapping that the matched field should use.
  32. Templates are processed in order -- the first matching template wins. New
  33. templates can be appended to the end of the list with the
  34. <<indices-put-mapping,PUT mapping>> API. If a new template has the same
  35. name as an existing template, it will replace the old version.
  36. [[match-mapping-type]]
  37. ==== `match_mapping_type`
  38. The `match_mapping_type` matches on the datatype detected by
  39. <<dynamic-field-mapping,dynamic field mapping>>, in other words, the datatype
  40. that Elasticsearch thinks the field should have. Only the following datatypes
  41. can be automatically detected: `boolean`, `date`, `double`, `long`, `object`,
  42. `string`. It also accepts `*` to match all datatypes.
  43. For example, if we wanted to map all integer fields as `integer` instead of
  44. `long`, and all `string` fields as both `text` and `keyword`, we
  45. could use the following template:
  46. [source,js]
  47. --------------------------------------------------
  48. PUT my_index
  49. {
  50. "mappings": {
  51. "my_type": {
  52. "dynamic_templates": [
  53. {
  54. "integers": {
  55. "match_mapping_type": "long",
  56. "mapping": {
  57. "type": "integer"
  58. }
  59. }
  60. },
  61. {
  62. "strings": {
  63. "match_mapping_type": "string",
  64. "mapping": {
  65. "type": "text",
  66. "fields": {
  67. "raw": {
  68. "type": "keyword",
  69. "ignore_above": 256
  70. }
  71. }
  72. }
  73. }
  74. }
  75. ]
  76. }
  77. }
  78. }
  79. PUT my_index/my_type/1
  80. {
  81. "my_integer": 5, <1>
  82. "my_string": "Some string" <2>
  83. }
  84. --------------------------------------------------
  85. // AUTOSENSE
  86. <1> The `my_integer` field is mapped as an `integer`.
  87. <2> The `my_string` field is mapped as a `text`, with a `keyword` <<multi-fields,multi field>>.
  88. [[match-unmatch]]
  89. ==== `match` and `unmatch`
  90. The `match` parameter uses a pattern to match on the fieldname, while
  91. `unmatch` uses a pattern to exclude fields matched by `match`.
  92. The following example matches all `string` fields whose name starts with
  93. `long_` (except for those which end with `_text`) and maps them as `long`
  94. fields:
  95. [source,js]
  96. --------------------------------------------------
  97. PUT my_index
  98. {
  99. "mappings": {
  100. "my_type": {
  101. "dynamic_templates": [
  102. {
  103. "longs_as_strings": {
  104. "match_mapping_type": "string",
  105. "match": "long_*",
  106. "unmatch": "*_text",
  107. "mapping": {
  108. "type": "long"
  109. }
  110. }
  111. }
  112. ]
  113. }
  114. }
  115. }
  116. PUT my_index/my_type/1
  117. {
  118. "long_num": "5", <1>
  119. "long_text": "foo" <2>
  120. }
  121. --------------------------------------------------
  122. // AUTOSENSE
  123. <1> The `long_num` field is mapped as a `long`.
  124. <2> The `long_text` field uses the default `string` mapping.
  125. [[match-pattern]]
  126. ==== `match_pattern`
  127. The `match_pattern` parameter adjusts the behavior of the `match` parameter
  128. such that it supports full Java regular expression matching on the field name
  129. instead of simple wildcards, for instance:
  130. [source,js]
  131. --------------------------------------------------
  132. "match_pattern": "regex",
  133. "match": "^profit_\d+$"
  134. --------------------------------------------------
  135. [[path-match-unmatch]]
  136. ==== `path_match` and `path_unmatch`
  137. The `path_match` and `path_unmatch` parameters work in the same way as `match`
  138. and `unmatch`, but operate on the full dotted path to the field, not just the
  139. final name, e.g. `some_object.*.some_field`.
  140. This example copies the values of any fields in the `name` object to the
  141. top-level `full_name` field, except for the `middle` field:
  142. [source,js]
  143. --------------------------------------------------
  144. PUT my_index
  145. {
  146. "mappings": {
  147. "my_type": {
  148. "dynamic_templates": [
  149. {
  150. "full_name": {
  151. "path_match": "name.*",
  152. "path_unmatch": "*.middle",
  153. "mapping": {
  154. "type": "text",
  155. "copy_to": "full_name"
  156. }
  157. }
  158. }
  159. ]
  160. }
  161. }
  162. }
  163. PUT my_index/my_type/1
  164. {
  165. "name": {
  166. "first": "Alice",
  167. "middle": "Mary",
  168. "last": "White"
  169. }
  170. }
  171. --------------------------------------------------
  172. // AUTOSENSE
  173. [[template-variables]]
  174. ==== `{name}` and `{dynamic_type}`
  175. The `{name}` and `{dynamic_type}` placeholders are replaced in the `mapping`
  176. with the field name and detected dynamic type. The following example sets all
  177. string fields to use an <<analyzer,`analyzer`>> with the same name as the
  178. field, and disables <<doc-values,`doc_values`>> for all non-string fields:
  179. [source,js]
  180. --------------------------------------------------
  181. PUT my_index
  182. {
  183. "mappings": {
  184. "my_type": {
  185. "dynamic_templates": [
  186. {
  187. "named_analyzers": {
  188. "match_mapping_type": "string",
  189. "match": "*",
  190. "mapping": {
  191. "type": "text",
  192. "analyzer": "{name}"
  193. }
  194. }
  195. },
  196. {
  197. "no_doc_values": {
  198. "match_mapping_type":"*",
  199. "mapping": {
  200. "type": "{dynamic_type}",
  201. "doc_values": false
  202. }
  203. }
  204. }
  205. ]
  206. }
  207. }
  208. }
  209. PUT my_index/my_type/1
  210. {
  211. "english": "Some English text", <1>
  212. "count": 5 <2>
  213. }
  214. --------------------------------------------------
  215. // AUTOSENSE
  216. <1> The `english` field is mapped as a `string` field with the `english` analyzer.
  217. <2> The `count` field is mapped as a `long` field with `doc_values` disabled
  218. [[template-examples]]
  219. ==== Template examples
  220. Here are some examples of potentially useful dynamic templates:
  221. ===== Structured search
  222. By default elasticsearch will map string fields as a `text` field with a sub
  223. `keyword` field. However if you are only indexing structured content and not
  224. interested in full text search, you can make elasticsearch map your fields
  225. only as `keyword`s. Note that this means that in order to search those fields,
  226. you will have to search on the exact same value that was indexed.
  227. [source,js]
  228. --------------------------------------------------
  229. PUT my_index
  230. {
  231. "mappings": {
  232. "my_type": {
  233. "dynamic_templates": [
  234. {
  235. "strings_as_keywords": {
  236. "match_mapping_type": "string",
  237. "mapping": {
  238. "type": "keyword"
  239. }
  240. }
  241. }
  242. ]
  243. }
  244. }
  245. }
  246. --------------------------------------------------
  247. ===== `text`-only mappings for strings
  248. On the contrary to the previous example, if the only thing that you care about
  249. on your string fields is full-text search, and if you don't plan on running
  250. aggregations, sorting or exact search on your string fields, you could tell
  251. elasticsearch to map it only as a text field (which was the default behaviour
  252. before 5.0):
  253. [source,js]
  254. --------------------------------------------------
  255. PUT my_index
  256. {
  257. "mappings": {
  258. "my_type": {
  259. "dynamic_templates": [
  260. {
  261. "strings_as_text": {
  262. "match_mapping_type": "string",
  263. "mapping": {
  264. "type": "text"
  265. }
  266. }
  267. }
  268. ]
  269. }
  270. }
  271. }
  272. --------------------------------------------------
  273. ===== Disabled norms
  274. Norms are index-time scoring factors. If you do not care about scoring, which
  275. would be the case for instance if you never sort documents by score, you could
  276. disable the storage of these scoring factors in the index and save some space.
  277. [source,js]
  278. --------------------------------------------------
  279. PUT my_index
  280. {
  281. "mappings": {
  282. "my_type": {
  283. "dynamic_templates": [
  284. {
  285. "strings_as_keywords": {
  286. "match_mapping_type": "string",
  287. "mapping": {
  288. "type": "text",
  289. "norms": false,
  290. "fields": {
  291. "keyword": {
  292. "type": "keyword",
  293. "ignore_above": 256
  294. }
  295. }
  296. }
  297. }
  298. }
  299. ]
  300. }
  301. }
  302. }
  303. --------------------------------------------------
  304. The sub `keyword` field appears in this template to be consistent with the
  305. default rules of dynamic mappings. Of course if you do not need them because
  306. you don't need to perform exact search or aggregate on this field, you could
  307. remove it as described in the previous section.
  308. ===== Time-series
  309. When doing time series analysis with elastisearch, it is common to have many
  310. numeric fields that you will often aggregate on but never filter on. In such a
  311. case, you could disable indexing on those fields to save disk space and also
  312. maybe gain some indexing speed:
  313. [source,js]
  314. --------------------------------------------------
  315. PUT my_index
  316. {
  317. "mappings": {
  318. "my_type": {
  319. "dynamic_templates": [
  320. {
  321. "unindexed_longs": {
  322. "match_mapping_type": "long",
  323. "mapping": {
  324. "type": "long",
  325. "index": false
  326. }
  327. }
  328. },
  329. {
  330. "unindexed_doubles": {
  331. "match_mapping_type": "double",
  332. "mapping": {
  333. "type": "float", <1>
  334. "index": false
  335. }
  336. }
  337. }
  338. ]
  339. }
  340. }
  341. }
  342. --------------------------------------------------
  343. <1> Like the default dynamic mapping rules, doubles are mapped as floats, which
  344. are usually accurate enough, yet require half the disk space.
  345. [[override-default-template]]
  346. === Override default template
  347. You can override the default mappings for all indices and all types
  348. by specifying a `_default_` type mapping in an index template
  349. which matches all indices.
  350. For example, to disable the `_all` field by default for all types in all
  351. new indices, you could create the following index template:
  352. [source,js]
  353. --------------------------------------------------
  354. PUT _template/disable_all_field
  355. {
  356. "order": 0,
  357. "template": "*", <1>
  358. "mappings": {
  359. "_default_": { <2>
  360. "_all": { <3>
  361. "enabled": false
  362. }
  363. }
  364. }
  365. }
  366. --------------------------------------------------
  367. // AUTOSENSE
  368. <1> Applies the mappings to an `index` which matches the pattern `*`, in other
  369. words, all new indices.
  370. <2> Defines the `_default_` type mapping types within the index.
  371. <3> Disables the `_all` field by default.