1
0

templates.asciidoc 11 KB

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