templates.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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": "John",
  171. "middle": "Winston",
  172. "last": "Lennon"
  173. }
  174. }
  175. --------------------------------------------------
  176. // CONSOLE
  177. Note that the `path_match` and `path_unmatch` parameters match on object paths
  178. in addition to leaf fields. As an example, indexing the following document will
  179. result in an error because the `path_match` setting also matches the object
  180. field `name.title`, which can't be mapped as text:
  181. [source,js]
  182. --------------------------------------------------
  183. PUT my_index/_doc/2
  184. {
  185. "name": {
  186. "first": "Paul",
  187. "last": "McCartney",
  188. "title": {
  189. "value": "Sir",
  190. "category": "order of chivalry"
  191. }
  192. }
  193. }
  194. --------------------------------------------------
  195. // CONSOLE
  196. // TEST[continued]
  197. // TEST[catch:bad_request]
  198. [[template-variables]]
  199. ==== `{name}` and `{dynamic_type}`
  200. The `{name}` and `{dynamic_type}` placeholders are replaced in the `mapping`
  201. with the field name and detected dynamic type. The following example sets all
  202. string fields to use an <<analyzer,`analyzer`>> with the same name as the
  203. field, and disables <<doc-values,`doc_values`>> for all non-string fields:
  204. [source,js]
  205. --------------------------------------------------
  206. PUT my_index
  207. {
  208. "mappings": {
  209. "dynamic_templates": [
  210. {
  211. "named_analyzers": {
  212. "match_mapping_type": "string",
  213. "match": "*",
  214. "mapping": {
  215. "type": "text",
  216. "analyzer": "{name}"
  217. }
  218. }
  219. },
  220. {
  221. "no_doc_values": {
  222. "match_mapping_type":"*",
  223. "mapping": {
  224. "type": "{dynamic_type}",
  225. "doc_values": false
  226. }
  227. }
  228. }
  229. ]
  230. }
  231. }
  232. PUT my_index/_doc/1
  233. {
  234. "english": "Some English text", <1>
  235. "count": 5 <2>
  236. }
  237. --------------------------------------------------
  238. // CONSOLE
  239. <1> The `english` field is mapped as a `string` field with the `english` analyzer.
  240. <2> The `count` field is mapped as a `long` field with `doc_values` disabled.
  241. [[template-examples]]
  242. ==== Template examples
  243. Here are some examples of potentially useful dynamic templates:
  244. ===== Structured search
  245. By default Elasticsearch will map string fields as a `text` field with a sub
  246. `keyword` field. However if you are only indexing structured content and not
  247. interested in full text search, you can make Elasticsearch map your fields
  248. only as `keyword`s. Note that this means that in order to search those fields,
  249. you will have to search on the exact same value that was indexed.
  250. [source,js]
  251. --------------------------------------------------
  252. PUT my_index
  253. {
  254. "mappings": {
  255. "dynamic_templates": [
  256. {
  257. "strings_as_keywords": {
  258. "match_mapping_type": "string",
  259. "mapping": {
  260. "type": "keyword"
  261. }
  262. }
  263. }
  264. ]
  265. }
  266. }
  267. --------------------------------------------------
  268. // CONSOLE
  269. [[text-only-mappings-strings]]
  270. ===== `text`-only mappings for strings
  271. On the contrary to the previous example, if the only thing that you care about
  272. on your string fields is full-text search, and if you don't plan on running
  273. aggregations, sorting or exact search on your string fields, you could tell
  274. Elasticsearch to map it only as a text field (which was the default behaviour
  275. before 5.0):
  276. [source,js]
  277. --------------------------------------------------
  278. PUT my_index
  279. {
  280. "mappings": {
  281. "dynamic_templates": [
  282. {
  283. "strings_as_text": {
  284. "match_mapping_type": "string",
  285. "mapping": {
  286. "type": "text"
  287. }
  288. }
  289. }
  290. ]
  291. }
  292. }
  293. --------------------------------------------------
  294. // CONSOLE
  295. ===== Disabled norms
  296. Norms are index-time scoring factors. If you do not care about scoring, which
  297. would be the case for instance if you never sort documents by score, you could
  298. disable the storage of these scoring factors in the index and save some space.
  299. [source,js]
  300. --------------------------------------------------
  301. PUT my_index
  302. {
  303. "mappings": {
  304. "dynamic_templates": [
  305. {
  306. "strings_as_keywords": {
  307. "match_mapping_type": "string",
  308. "mapping": {
  309. "type": "text",
  310. "norms": false,
  311. "fields": {
  312. "keyword": {
  313. "type": "keyword",
  314. "ignore_above": 256
  315. }
  316. }
  317. }
  318. }
  319. }
  320. ]
  321. }
  322. }
  323. --------------------------------------------------
  324. // CONSOLE
  325. The sub `keyword` field appears in this template to be consistent with the
  326. default rules of dynamic mappings. Of course if you do not need them because
  327. you don't need to perform exact search or aggregate on this field, you could
  328. remove it as described in the previous section.
  329. ===== Time-series
  330. When doing time series analysis with Elasticsearch, it is common to have many
  331. numeric fields that you will often aggregate on but never filter on. In such a
  332. case, you could disable indexing on those fields to save disk space and also
  333. maybe gain some indexing speed:
  334. [source,js]
  335. --------------------------------------------------
  336. PUT my_index
  337. {
  338. "mappings": {
  339. "dynamic_templates": [
  340. {
  341. "unindexed_longs": {
  342. "match_mapping_type": "long",
  343. "mapping": {
  344. "type": "long",
  345. "index": false
  346. }
  347. }
  348. },
  349. {
  350. "unindexed_doubles": {
  351. "match_mapping_type": "double",
  352. "mapping": {
  353. "type": "float", <1>
  354. "index": false
  355. }
  356. }
  357. }
  358. ]
  359. }
  360. }
  361. --------------------------------------------------
  362. // CONSOLE
  363. <1> Like the default dynamic mapping rules, doubles are mapped as floats, which
  364. are usually accurate enough, yet require half the disk space.