templates.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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,console]
  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. <1> The `my_integer` field is mapped as an `integer`.
  93. <2> The `my_string` field is mapped as a `text`, with a `keyword` <<multi-fields,multi field>>.
  94. [[match-unmatch]]
  95. ==== `match` and `unmatch`
  96. The `match` parameter uses a pattern to match on the field name, while
  97. `unmatch` uses a pattern to exclude fields matched by `match`.
  98. The following example matches all `string` fields whose name starts with
  99. `long_` (except for those which end with `_text`) and maps them as `long`
  100. fields:
  101. [source,console]
  102. --------------------------------------------------
  103. PUT my_index
  104. {
  105. "mappings": {
  106. "dynamic_templates": [
  107. {
  108. "longs_as_strings": {
  109. "match_mapping_type": "string",
  110. "match": "long_*",
  111. "unmatch": "*_text",
  112. "mapping": {
  113. "type": "long"
  114. }
  115. }
  116. }
  117. ]
  118. }
  119. }
  120. PUT my_index/_doc/1
  121. {
  122. "long_num": "5", <1>
  123. "long_text": "foo" <2>
  124. }
  125. --------------------------------------------------
  126. <1> The `long_num` field is mapped as a `long`.
  127. <2> The `long_text` field uses the default `string` mapping.
  128. [[match-pattern]]
  129. ==== `match_pattern`
  130. The `match_pattern` parameter adjusts the behavior of the `match` parameter
  131. such that it supports full Java regular expression matching on the field name
  132. instead of simple wildcards, for instance:
  133. [source,js]
  134. --------------------------------------------------
  135. "match_pattern": "regex",
  136. "match": "^profit_\d+$"
  137. --------------------------------------------------
  138. // NOTCONSOLE
  139. [[path-match-unmatch]]
  140. ==== `path_match` and `path_unmatch`
  141. The `path_match` and `path_unmatch` parameters work in the same way as `match`
  142. and `unmatch`, but operate on the full dotted path to the field, not just the
  143. final name, e.g. `some_object.*.some_field`.
  144. This example copies the values of any fields in the `name` object to the
  145. top-level `full_name` field, except for the `middle` field:
  146. [source,console]
  147. --------------------------------------------------
  148. PUT my_index
  149. {
  150. "mappings": {
  151. "dynamic_templates": [
  152. {
  153. "full_name": {
  154. "path_match": "name.*",
  155. "path_unmatch": "*.middle",
  156. "mapping": {
  157. "type": "text",
  158. "copy_to": "full_name"
  159. }
  160. }
  161. }
  162. ]
  163. }
  164. }
  165. PUT my_index/_doc/1
  166. {
  167. "name": {
  168. "first": "John",
  169. "middle": "Winston",
  170. "last": "Lennon"
  171. }
  172. }
  173. --------------------------------------------------
  174. Note that the `path_match` and `path_unmatch` parameters match on object paths
  175. in addition to leaf fields. As an example, indexing the following document will
  176. result in an error because the `path_match` setting also matches the object
  177. field `name.title`, which can't be mapped as text:
  178. [source,console]
  179. --------------------------------------------------
  180. PUT my_index/_doc/2
  181. {
  182. "name": {
  183. "first": "Paul",
  184. "last": "McCartney",
  185. "title": {
  186. "value": "Sir",
  187. "category": "order of chivalry"
  188. }
  189. }
  190. }
  191. --------------------------------------------------
  192. // TEST[continued]
  193. // TEST[catch:bad_request]
  194. [[template-variables]]
  195. ==== `{name}` and `{dynamic_type}`
  196. The `{name}` and `{dynamic_type}` placeholders are replaced in the `mapping`
  197. with the field name and detected dynamic type. The following example sets all
  198. string fields to use an <<analyzer,`analyzer`>> with the same name as the
  199. field, and disables <<doc-values,`doc_values`>> for all non-string fields:
  200. [source,console]
  201. --------------------------------------------------
  202. PUT my_index
  203. {
  204. "mappings": {
  205. "dynamic_templates": [
  206. {
  207. "named_analyzers": {
  208. "match_mapping_type": "string",
  209. "match": "*",
  210. "mapping": {
  211. "type": "text",
  212. "analyzer": "{name}"
  213. }
  214. }
  215. },
  216. {
  217. "no_doc_values": {
  218. "match_mapping_type":"*",
  219. "mapping": {
  220. "type": "{dynamic_type}",
  221. "doc_values": false
  222. }
  223. }
  224. }
  225. ]
  226. }
  227. }
  228. PUT my_index/_doc/1
  229. {
  230. "english": "Some English text", <1>
  231. "count": 5 <2>
  232. }
  233. --------------------------------------------------
  234. <1> The `english` field is mapped as a `string` field with the `english` analyzer.
  235. <2> The `count` field is mapped as a `long` field with `doc_values` disabled.
  236. [[template-examples]]
  237. ==== Template examples
  238. Here are some examples of potentially useful dynamic templates:
  239. ===== Structured search
  240. By default Elasticsearch will map string fields as a `text` field with a sub
  241. `keyword` field. However if you are only indexing structured content and not
  242. interested in full text search, you can make Elasticsearch map your fields
  243. only as `keyword`s. Note that this means that in order to search those fields,
  244. you will have to search on the exact same value that was indexed.
  245. [source,console]
  246. --------------------------------------------------
  247. PUT my_index
  248. {
  249. "mappings": {
  250. "dynamic_templates": [
  251. {
  252. "strings_as_keywords": {
  253. "match_mapping_type": "string",
  254. "mapping": {
  255. "type": "keyword"
  256. }
  257. }
  258. }
  259. ]
  260. }
  261. }
  262. --------------------------------------------------
  263. [[text-only-mappings-strings]]
  264. ===== `text`-only mappings for strings
  265. On the contrary to the previous example, if the only thing that you care about
  266. on your string fields is full-text search, and if you don't plan on running
  267. aggregations, sorting or exact search on your string fields, you could tell
  268. Elasticsearch to map it only as a text field (which was the default behaviour
  269. before 5.0):
  270. [source,console]
  271. --------------------------------------------------
  272. PUT my_index
  273. {
  274. "mappings": {
  275. "dynamic_templates": [
  276. {
  277. "strings_as_text": {
  278. "match_mapping_type": "string",
  279. "mapping": {
  280. "type": "text"
  281. }
  282. }
  283. }
  284. ]
  285. }
  286. }
  287. --------------------------------------------------
  288. ===== Disabled norms
  289. Norms are index-time scoring factors. If you do not care about scoring, which
  290. would be the case for instance if you never sort documents by score, you could
  291. disable the storage of these scoring factors in the index and save some space.
  292. [source,console]
  293. --------------------------------------------------
  294. PUT my_index
  295. {
  296. "mappings": {
  297. "dynamic_templates": [
  298. {
  299. "strings_as_keywords": {
  300. "match_mapping_type": "string",
  301. "mapping": {
  302. "type": "text",
  303. "norms": false,
  304. "fields": {
  305. "keyword": {
  306. "type": "keyword",
  307. "ignore_above": 256
  308. }
  309. }
  310. }
  311. }
  312. }
  313. ]
  314. }
  315. }
  316. --------------------------------------------------
  317. The sub `keyword` field appears in this template to be consistent with the
  318. default rules of dynamic mappings. Of course if you do not need them because
  319. you don't need to perform exact search or aggregate on this field, you could
  320. remove it as described in the previous section.
  321. ===== Time-series
  322. When doing time series analysis with Elasticsearch, it is common to have many
  323. numeric fields that you will often aggregate on but never filter on. In such a
  324. case, you could disable indexing on those fields to save disk space and also
  325. maybe gain some indexing speed:
  326. [source,console]
  327. --------------------------------------------------
  328. PUT my_index
  329. {
  330. "mappings": {
  331. "dynamic_templates": [
  332. {
  333. "unindexed_longs": {
  334. "match_mapping_type": "long",
  335. "mapping": {
  336. "type": "long",
  337. "index": false
  338. }
  339. }
  340. },
  341. {
  342. "unindexed_doubles": {
  343. "match_mapping_type": "double",
  344. "mapping": {
  345. "type": "float", <1>
  346. "index": false
  347. }
  348. }
  349. }
  350. ]
  351. }
  352. }
  353. --------------------------------------------------
  354. <1> Like the default dynamic mapping rules, doubles are mapped as floats, which
  355. are usually accurate enough, yet require half the disk space.