templates.asciidoc 11 KB

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