templates.asciidoc 12 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. // NOTCONSOLE
  30. <1> The template name can be any string value.
  31. <2> The match conditions can include any of : `match_mapping_type`, `match`, `match_pattern`, `unmatch`, `path_match`, `path_unmatch`.
  32. <3> The mapping that the matched field should use.
  33. If a provided mapping contains an invalid mapping snippet then that results in
  34. a validation error. Validation always occurs when applying the dynamic template
  35. at index time or in most cases when updating the dynamic template.
  36. Whether updating the dynamic template fails when supplying an invalid mapping snippet depends on the following:
  37. * If no `match_mapping_type` has been specified then if the template is valid with one predefined mapping type then
  38. the mapping snippet is considered valid. However if at index time a field that matches with the template is indexed
  39. as a different type then an validation error will occur at index time instead. For example configuring a dynamic
  40. template with no `match_mapping_type` is considered valid as string type, but at index time a field that matches with
  41. the dynamic template is indexed as a long, then at index time a validation error may still occur.
  42. * If the `{{name}}` placeholder is used in the mapping snippet then the validation is skipped when updating
  43. the dynamic template. This is because the field name is unknown at that time. The validation will then occur
  44. when applying the template at index time.
  45. Templates are processed in order -- the first matching template wins. When
  46. putting new dynamic templates through the <<indices-put-mapping, put mapping>> API,
  47. all existing templates are overwritten. This allows for dynamic templates to be
  48. reordered or deleted after they were initially added.
  49. [[match-mapping-type]]
  50. ==== `match_mapping_type`
  51. The `match_mapping_type` is the datatype detected by the json parser. Since
  52. JSON doesn't allow to distinguish a `long` from an `integer` or a `double` from
  53. a `float`, it will always choose the wider datatype, i.e. `long` for integers
  54. and `double` for floating-point numbers.
  55. The following datatypes may be automatically detected:
  56. - `boolean` when `true` or `false` are encountered.
  57. - `date` when <<date-detection,date detection>> is enabled and a string is
  58. found that matches any of the configured date formats.
  59. - `double` for numbers with a decimal part.
  60. - `long` for numbers without a decimal part.
  61. - `object` for objects, also called hashes.
  62. - `string` for character strings.
  63. `*` may also be used in order to match all datatypes.
  64. For example, if we wanted to map all integer fields as `integer` instead of
  65. `long`, and all `string` fields as both `text` and `keyword`, we
  66. could use the following template:
  67. [source,console]
  68. --------------------------------------------------
  69. PUT my_index
  70. {
  71. "mappings": {
  72. "dynamic_templates": [
  73. {
  74. "integers": {
  75. "match_mapping_type": "long",
  76. "mapping": {
  77. "type": "integer"
  78. }
  79. }
  80. },
  81. {
  82. "strings": {
  83. "match_mapping_type": "string",
  84. "mapping": {
  85. "type": "text",
  86. "fields": {
  87. "raw": {
  88. "type": "keyword",
  89. "ignore_above": 256
  90. }
  91. }
  92. }
  93. }
  94. }
  95. ]
  96. }
  97. }
  98. PUT my_index/_doc/1
  99. {
  100. "my_integer": 5, <1>
  101. "my_string": "Some string" <2>
  102. }
  103. --------------------------------------------------
  104. <1> The `my_integer` field is mapped as an `integer`.
  105. <2> The `my_string` field is mapped as a `text`, with a `keyword` <<multi-fields,multi field>>.
  106. [[match-unmatch]]
  107. ==== `match` and `unmatch`
  108. The `match` parameter uses a pattern to match on the field name, while
  109. `unmatch` uses a pattern to exclude fields matched by `match`.
  110. The following example matches all `string` fields whose name starts with
  111. `long_` (except for those which end with `_text`) and maps them as `long`
  112. fields:
  113. [source,console]
  114. --------------------------------------------------
  115. PUT my_index
  116. {
  117. "mappings": {
  118. "dynamic_templates": [
  119. {
  120. "longs_as_strings": {
  121. "match_mapping_type": "string",
  122. "match": "long_*",
  123. "unmatch": "*_text",
  124. "mapping": {
  125. "type": "long"
  126. }
  127. }
  128. }
  129. ]
  130. }
  131. }
  132. PUT my_index/_doc/1
  133. {
  134. "long_num": "5", <1>
  135. "long_text": "foo" <2>
  136. }
  137. --------------------------------------------------
  138. <1> The `long_num` field is mapped as a `long`.
  139. <2> The `long_text` field uses the default `string` mapping.
  140. [[match-pattern]]
  141. ==== `match_pattern`
  142. The `match_pattern` parameter adjusts the behavior of the `match` parameter
  143. such that it supports full Java regular expression matching on the field name
  144. instead of simple wildcards, for instance:
  145. [source,js]
  146. --------------------------------------------------
  147. "match_pattern": "regex",
  148. "match": "^profit_\d+$"
  149. --------------------------------------------------
  150. // NOTCONSOLE
  151. [[path-match-unmatch]]
  152. ==== `path_match` and `path_unmatch`
  153. The `path_match` and `path_unmatch` parameters work in the same way as `match`
  154. and `unmatch`, but operate on the full dotted path to the field, not just the
  155. final name, e.g. `some_object.*.some_field`.
  156. This example copies the values of any fields in the `name` object to the
  157. top-level `full_name` field, except for the `middle` field:
  158. [source,console]
  159. --------------------------------------------------
  160. PUT my_index
  161. {
  162. "mappings": {
  163. "dynamic_templates": [
  164. {
  165. "full_name": {
  166. "path_match": "name.*",
  167. "path_unmatch": "*.middle",
  168. "mapping": {
  169. "type": "text",
  170. "copy_to": "full_name"
  171. }
  172. }
  173. }
  174. ]
  175. }
  176. }
  177. PUT my_index/_doc/1
  178. {
  179. "name": {
  180. "first": "John",
  181. "middle": "Winston",
  182. "last": "Lennon"
  183. }
  184. }
  185. --------------------------------------------------
  186. Note that the `path_match` and `path_unmatch` parameters match on object paths
  187. in addition to leaf fields. As an example, indexing the following document will
  188. result in an error because the `path_match` setting also matches the object
  189. field `name.title`, which can't be mapped as text:
  190. [source,console]
  191. --------------------------------------------------
  192. PUT my_index/_doc/2
  193. {
  194. "name": {
  195. "first": "Paul",
  196. "last": "McCartney",
  197. "title": {
  198. "value": "Sir",
  199. "category": "order of chivalry"
  200. }
  201. }
  202. }
  203. --------------------------------------------------
  204. // TEST[continued]
  205. // TEST[catch:bad_request]
  206. [[template-variables]]
  207. ==== `{name}` and `{dynamic_type}`
  208. The `{name}` and `{dynamic_type}` placeholders are replaced in the `mapping`
  209. with the field name and detected dynamic type. The following example sets all
  210. string fields to use an <<analyzer,`analyzer`>> with the same name as the
  211. field, and disables <<doc-values,`doc_values`>> for all non-string fields:
  212. [source,console]
  213. --------------------------------------------------
  214. PUT my_index
  215. {
  216. "mappings": {
  217. "dynamic_templates": [
  218. {
  219. "named_analyzers": {
  220. "match_mapping_type": "string",
  221. "match": "*",
  222. "mapping": {
  223. "type": "text",
  224. "analyzer": "{name}"
  225. }
  226. }
  227. },
  228. {
  229. "no_doc_values": {
  230. "match_mapping_type":"*",
  231. "mapping": {
  232. "type": "{dynamic_type}",
  233. "doc_values": false
  234. }
  235. }
  236. }
  237. ]
  238. }
  239. }
  240. PUT my_index/_doc/1
  241. {
  242. "english": "Some English text", <1>
  243. "count": 5 <2>
  244. }
  245. --------------------------------------------------
  246. <1> The `english` field is mapped as a `string` field with the `english` analyzer.
  247. <2> The `count` field is mapped as a `long` field with `doc_values` disabled.
  248. [[template-examples]]
  249. ==== Template examples
  250. Here are some examples of potentially useful dynamic templates:
  251. ===== Structured search
  252. By default Elasticsearch will map string fields as a `text` field with a sub
  253. `keyword` field. However if you are only indexing structured content and not
  254. interested in full text search, you can make Elasticsearch map your fields
  255. only as `keyword`s. Note that this means that in order to search those fields,
  256. you will have to search on the exact same value that was indexed.
  257. [source,console]
  258. --------------------------------------------------
  259. PUT my_index
  260. {
  261. "mappings": {
  262. "dynamic_templates": [
  263. {
  264. "strings_as_keywords": {
  265. "match_mapping_type": "string",
  266. "mapping": {
  267. "type": "keyword"
  268. }
  269. }
  270. }
  271. ]
  272. }
  273. }
  274. --------------------------------------------------
  275. [[text-only-mappings-strings]]
  276. ===== `text`-only mappings for strings
  277. On the contrary to the previous example, if the only thing that you care about
  278. on your string fields is full-text search, and if you don't plan on running
  279. aggregations, sorting or exact search on your string fields, you could tell
  280. Elasticsearch to map it only as a text field (which was the default behaviour
  281. before 5.0):
  282. [source,console]
  283. --------------------------------------------------
  284. PUT my_index
  285. {
  286. "mappings": {
  287. "dynamic_templates": [
  288. {
  289. "strings_as_text": {
  290. "match_mapping_type": "string",
  291. "mapping": {
  292. "type": "text"
  293. }
  294. }
  295. }
  296. ]
  297. }
  298. }
  299. --------------------------------------------------
  300. ===== Disabled norms
  301. Norms are index-time scoring factors. If you do not care about scoring, which
  302. would be the case for instance if you never sort documents by score, you could
  303. disable the storage of these scoring factors in the index and save some space.
  304. [source,console]
  305. --------------------------------------------------
  306. PUT my_index
  307. {
  308. "mappings": {
  309. "dynamic_templates": [
  310. {
  311. "strings_as_keywords": {
  312. "match_mapping_type": "string",
  313. "mapping": {
  314. "type": "text",
  315. "norms": false,
  316. "fields": {
  317. "keyword": {
  318. "type": "keyword",
  319. "ignore_above": 256
  320. }
  321. }
  322. }
  323. }
  324. }
  325. ]
  326. }
  327. }
  328. --------------------------------------------------
  329. The sub `keyword` field appears in this template to be consistent with the
  330. default rules of dynamic mappings. Of course if you do not need them because
  331. you don't need to perform exact search or aggregate on this field, you could
  332. remove it as described in the previous section.
  333. ===== Time-series
  334. When doing time series analysis with Elasticsearch, it is common to have many
  335. numeric fields that you will often aggregate on but never filter on. In such a
  336. case, you could disable indexing on those fields to save disk space and also
  337. maybe gain some indexing speed:
  338. [source,console]
  339. --------------------------------------------------
  340. PUT my_index
  341. {
  342. "mappings": {
  343. "dynamic_templates": [
  344. {
  345. "unindexed_longs": {
  346. "match_mapping_type": "long",
  347. "mapping": {
  348. "type": "long",
  349. "index": false
  350. }
  351. }
  352. },
  353. {
  354. "unindexed_doubles": {
  355. "match_mapping_type": "double",
  356. "mapping": {
  357. "type": "float", <1>
  358. "index": false
  359. }
  360. }
  361. }
  362. ]
  363. }
  364. }
  365. --------------------------------------------------
  366. <1> Like the default dynamic mapping rules, doubles are mapped as floats, which
  367. are usually accurate enough, yet require half the disk space.