templates.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. [[dynamic-templates]]
  2. === Dynamic templates
  3. Dynamic templates allow you greater control of how {es} maps your data beyond
  4. the default <<dynamic-field-mapping,dynamic field mapping rules>>. You enable
  5. dynamic mapping by setting the dynamic parameter to `true` or `runtime`. You
  6. can then use dynamic templates to define custom mappings that can be applied to
  7. dynamically added fields based on the matching condition:
  8. * <<match-mapping-type,`match_mapping_type`>> operates on the data type that
  9. {es} detects
  10. * <<match-unmatch,`match` and `unmatch`>> use a pattern to match on the field
  11. name
  12. * <<path-match-unmatch,`path_match` and `path_unmatch`>> operate on the full
  13. dotted path to the field
  14. * if a dynamic template does not define `match_mapping_type` nor `match` nor
  15. `path_match`, then it won't match any field, but it can be referred to by
  16. name in `dynamic_templates` as part of a <bulk, bulk request>.
  17. Use the `{name}` and `{dynamic_type}` <<template-variables,template variables>>
  18. in the mapping specification as placeholders.
  19. IMPORTANT: Dynamic field mappings are only added when a field contains a
  20. concrete value -- not `null` or an empty array. If the
  21. `null_value` option is used in a `dynamic_template`, it will only be applied
  22. after the first document with a concrete value for the field has been
  23. indexed.
  24. Dynamic templates are specified as an array of named objects:
  25. [source,js]
  26. --------------------------------------------------
  27. "dynamic_templates": [
  28. {
  29. "my_template_name": { <1>
  30. ... match conditions ... <2>
  31. "mapping": { ... } <3>
  32. }
  33. },
  34. ...
  35. ]
  36. --------------------------------------------------
  37. // NOTCONSOLE
  38. <1> The template name can be any string value.
  39. <2> The match conditions can include any of : `match_mapping_type`, `match`, `match_pattern`, `unmatch`, `path_match`, `path_unmatch`.
  40. <3> The mapping that the matched field should use.
  41. [[dynamic-templates-validation]]
  42. ==== Validating dynamic templates
  43. If a provided mapping contains an invalid mapping snippet, a validation error
  44. is returned. Validation occurs when applying the dynamic template at index time,
  45. and, in most cases, when the dynamic template is updated. Providing an invalid mapping
  46. snippet may cause the update or validation of a dynamic template to fail under certain conditions:
  47. * If no `match_mapping_type` has been specified but the template is valid for at least one predefined mapping type,
  48. the mapping snippet is considered valid. However, a validation error is returned at index time if a field matching
  49. the template is indexed as a different type. For example, configuring a dynamic template with no `match_mapping_type`
  50. is considered valid as string type, but if a field matching the dynamic template is indexed as a long, a validation
  51. error is returned at index time. It is recommended to configure the `match_mapping_type` to the expected JSON type or
  52. configure the desired `type` in the mapping snippet.
  53. * If the `{name}` placeholder is used in the mapping snippet, validation is skipped when updating the dynamic
  54. template. This is because the field name is unknown at that time. Instead, validation occurs when the template is applied
  55. at index time.
  56. Templates are processed in order -- the first matching template wins. When
  57. putting new dynamic templates through the <<indices-put-mapping, update mapping>> API,
  58. all existing templates are overwritten. This allows for dynamic templates to be
  59. reordered or deleted after they were initially added.
  60. [[dynamic-mapping-runtime-fields]]
  61. ==== Mapping runtime fields in a dynamic template
  62. If you want {es} to dynamically map new fields of a certain type as runtime
  63. fields, set `"dynamic":"runtime"` in the index mappings. These fields are not
  64. indexed, and are loaded from `_source` at query time.
  65. Alternatively, you can use the default dynamic mapping rules and then create
  66. dynamic templates to map specific fields as runtime fields. You set
  67. `"dynamic":"true"` in your index mapping, and then create a dynamic template to map
  68. new fields of a certain type as runtime fields.
  69. Let's say you have data where each of the fields start with `ip_`. Based on the
  70. <<match-mapping-type,dynamic mapping rules>>, {es} maps any `string` that passes
  71. `numeric` detection as a `float` or `long`. However, you can create a dynamic
  72. template that maps new strings as runtime fields of type `ip`.
  73. The following request defines a dynamic template named `strings_as_ip`. When
  74. {es} detects new `string` fields matching the `ip*` pattern, it maps those
  75. fields as runtime fields of type `ip`. Because `ip` fields aren't mapped
  76. dynamically, you can use this template with either `"dynamic":"true"` or
  77. `"dynamic":"runtime"`.
  78. [source,console]
  79. ----
  80. PUT my-index-000001/
  81. {
  82. "mappings": {
  83. "dynamic_templates": [
  84. {
  85. "strings_as_ip": {
  86. "match_mapping_type": "string",
  87. "match": "ip*",
  88. "runtime": {
  89. "type": "ip"
  90. }
  91. }
  92. }
  93. ]
  94. }
  95. }
  96. ----
  97. See <<text-only-mappings-strings,this example>> for how to use dynamic templates
  98. to map `string` fields as either indexed fields or runtime fields.
  99. [[match-mapping-type]]
  100. ==== `match_mapping_type`
  101. The `match_mapping_type` is the data type detected by the JSON parser. Because
  102. JSON doesn't distinguish a `long` from an `integer` or a `double` from
  103. a `float`, it always chooses the wider data type such as `long` for integers
  104. and `double` for floating-point numbers.
  105. {es} automatically detects the following data types:
  106. include::field-mapping.asciidoc[tag=dynamic-field-mapping-types-tag]
  107. Use a wildcard (`*`) to match all data types.
  108. For example, if we wanted to map all integer fields as `integer` instead of
  109. `long`, and all `string` fields as both `text` and `keyword`, we
  110. could use the following template:
  111. [source,console]
  112. --------------------------------------------------
  113. PUT my-index-000001
  114. {
  115. "mappings": {
  116. "dynamic_templates": [
  117. {
  118. "integers": {
  119. "match_mapping_type": "long",
  120. "mapping": {
  121. "type": "integer"
  122. }
  123. }
  124. },
  125. {
  126. "strings": {
  127. "match_mapping_type": "string",
  128. "mapping": {
  129. "type": "text",
  130. "fields": {
  131. "raw": {
  132. "type": "keyword",
  133. "ignore_above": 256
  134. }
  135. }
  136. }
  137. }
  138. }
  139. ]
  140. }
  141. }
  142. PUT my-index-000001/_doc/1
  143. {
  144. "my_integer": 5, <1>
  145. "my_string": "Some string" <2>
  146. }
  147. --------------------------------------------------
  148. <1> The `my_integer` field is mapped as an `integer`.
  149. <2> The `my_string` field is mapped as a `text`, with a `keyword` <<multi-fields,multi field>>.
  150. [[match-unmatch]]
  151. ==== `match` and `unmatch`
  152. The `match` parameter uses a pattern to match on the field name, while
  153. `unmatch` uses a pattern to exclude fields matched by `match`.
  154. The `match_pattern` parameter adjusts the behavior of the `match` parameter
  155. to support full Java regular expressions matching on the field name
  156. instead of simple wildcards. For example:
  157. [source,js]
  158. --------------------------------------------------
  159. "match_pattern": "regex",
  160. "match": "^profit_\d+$"
  161. --------------------------------------------------
  162. // NOTCONSOLE
  163. The following example matches all `string` fields whose name starts with
  164. `long_` (except for those which end with `_text`) and maps them as `long`
  165. fields:
  166. [source,console]
  167. --------------------------------------------------
  168. PUT my-index-000001
  169. {
  170. "mappings": {
  171. "dynamic_templates": [
  172. {
  173. "longs_as_strings": {
  174. "match_mapping_type": "string",
  175. "match": "long_*",
  176. "unmatch": "*_text",
  177. "mapping": {
  178. "type": "long"
  179. }
  180. }
  181. }
  182. ]
  183. }
  184. }
  185. PUT my-index-000001/_doc/1
  186. {
  187. "long_num": "5", <1>
  188. "long_text": "foo" <2>
  189. }
  190. --------------------------------------------------
  191. <1> The `long_num` field is mapped as a `long`.
  192. <2> The `long_text` field uses the default `string` mapping.
  193. [[path-match-unmatch]]
  194. ==== `path_match` and `path_unmatch`
  195. The `path_match` and `path_unmatch` parameters work in the same way as `match`
  196. and `unmatch`, but operate on the full dotted path to the field, not just the
  197. final name, e.g. `some_object.*.some_field`.
  198. This example copies the values of any fields in the `name` object to the
  199. top-level `full_name` field, except for the `middle` field:
  200. [source,console]
  201. --------------------------------------------------
  202. PUT my-index-000001
  203. {
  204. "mappings": {
  205. "dynamic_templates": [
  206. {
  207. "full_name": {
  208. "path_match": "name.*",
  209. "path_unmatch": "*.middle",
  210. "mapping": {
  211. "type": "text",
  212. "copy_to": "full_name"
  213. }
  214. }
  215. }
  216. ]
  217. }
  218. }
  219. PUT my-index-000001/_doc/1
  220. {
  221. "name": {
  222. "first": "John",
  223. "middle": "Winston",
  224. "last": "Lennon"
  225. }
  226. }
  227. --------------------------------------------------
  228. Note that the `path_match` and `path_unmatch` parameters match on object paths
  229. in addition to leaf fields. As an example, indexing the following document will
  230. result in an error because the `path_match` setting also matches the object
  231. field `name.title`, which can't be mapped as text:
  232. [source,console]
  233. ----
  234. PUT my-index-000001/_doc/2
  235. {
  236. "name": {
  237. "first": "Paul",
  238. "last": "McCartney",
  239. "title": {
  240. "value": "Sir",
  241. "category": "order of chivalry"
  242. }
  243. }
  244. }
  245. ----
  246. // TEST[continued]
  247. // TEST[catch:bad_request]
  248. [[template-variables]]
  249. ==== Template variables
  250. The `{name}` and `{dynamic_type}` placeholders are replaced in the `mapping`
  251. with the field name and detected dynamic type. The following example sets all
  252. string fields to use an <<analyzer,`analyzer`>> with the same name as the
  253. field, and disables <<doc-values,`doc_values`>> for all non-string fields:
  254. [source,console]
  255. ----
  256. PUT my-index-000001
  257. {
  258. "mappings": {
  259. "dynamic_templates": [
  260. {
  261. "named_analyzers": {
  262. "match_mapping_type": "string",
  263. "match": "*",
  264. "mapping": {
  265. "type": "text",
  266. "analyzer": "{name}"
  267. }
  268. }
  269. },
  270. {
  271. "no_doc_values": {
  272. "match_mapping_type":"*",
  273. "mapping": {
  274. "type": "{dynamic_type}",
  275. "doc_values": false
  276. }
  277. }
  278. }
  279. ]
  280. }
  281. }
  282. PUT my-index-000001/_doc/1
  283. {
  284. "english": "Some English text", <1>
  285. "count": 5 <2>
  286. }
  287. ----
  288. <1> The `english` field is mapped as a `string` field with the `english` analyzer.
  289. <2> The `count` field is mapped as a `long` field with `doc_values` disabled.
  290. [[template-examples]]
  291. ==== Dynamic template examples
  292. Here are some examples of potentially useful dynamic templates:
  293. ===== Structured search
  294. When you set `"dynamic":"true"`, {es} will map string fields as a `text` field with
  295. a `keyword` subfield. If you are only indexing structured content and not
  296. interested in full text search, you can make {es} map your fields
  297. only as `keyword` fields. However, you must search on the exact same value that
  298. was indexed to search those fields.
  299. [source,console]
  300. ----
  301. PUT my-index-000001
  302. {
  303. "mappings": {
  304. "dynamic_templates": [
  305. {
  306. "strings_as_keywords": {
  307. "match_mapping_type": "string",
  308. "mapping": {
  309. "type": "keyword"
  310. }
  311. }
  312. }
  313. ]
  314. }
  315. }
  316. ----
  317. [[text-only-mappings-strings]]
  318. ===== `text`-only mappings for strings
  319. Contrary to the previous example, if you only care about full-text search on
  320. string fields and don't plan on running aggregations, sorting, or exact
  321. searches, you could tell instruct {es} to map strings as `text`:
  322. [source,console]
  323. ----
  324. PUT my-index-000001
  325. {
  326. "mappings": {
  327. "dynamic_templates": [
  328. {
  329. "strings_as_text": {
  330. "match_mapping_type": "string",
  331. "mapping": {
  332. "type": "text"
  333. }
  334. }
  335. }
  336. ]
  337. }
  338. }
  339. ----
  340. Alternatively, you can create a dynamic template to map your string fields as
  341. `keyword` fields in the runtime section of the mapping. When {es} detects new
  342. fields of type `string`, those fields will be created as runtime fields of
  343. type `keyword`.
  344. Although your `string` fields won't be indexed, their values are stored in
  345. `_source` and can be used in search requests, aggregations, filtering, and
  346. sorting.
  347. For example, the following request creates a dynamic template to map `string`
  348. fields as runtime fields of type `keyword`. Although the `runtime` definition
  349. is blank, new `string` fields will be mapped as `keyword` runtime fields based
  350. on the <<dynamic-field-mapping-types,dynamic mapping rules>> that {es} uses for
  351. adding field types to the mapping. Any `string` that doesn't pass date
  352. detection or numeric detection is automatically mapped as a `keyword`:
  353. [source,console]
  354. ----
  355. PUT my-index-000001
  356. {
  357. "mappings": {
  358. "dynamic_templates": [
  359. {
  360. "strings_as_keywords": {
  361. "match_mapping_type": "string",
  362. "runtime": {}
  363. }
  364. }
  365. ]
  366. }
  367. }
  368. ----
  369. You index a simple document:
  370. [source,console]
  371. ----
  372. PUT my-index-000001/_doc/1
  373. {
  374. "english": "Some English text",
  375. "count": 5
  376. }
  377. ----
  378. //TEST[continued]
  379. When you view the mapping, you'll see that the `english` field is a runtime
  380. field of type `keyword`:
  381. [source,console]
  382. ----
  383. GET my-index-000001/_mapping
  384. ----
  385. //TEST[continued]
  386. [source,console-result]
  387. ----
  388. {
  389. "my-index-000001" : {
  390. "mappings" : {
  391. "dynamic_templates" : [
  392. {
  393. "strings_as_keywords" : {
  394. "match_mapping_type" : "string",
  395. "runtime" : { }
  396. }
  397. }
  398. ],
  399. "runtime" : {
  400. "english" : {
  401. "type" : "keyword"
  402. }
  403. },
  404. "properties" : {
  405. "count" : {
  406. "type" : "long"
  407. }
  408. }
  409. }
  410. }
  411. }
  412. ----
  413. ===== Disabled norms
  414. Norms are index-time scoring factors. If you do not care about scoring, which
  415. would be the case for instance if you never sort documents by score, you could
  416. disable the storage of these scoring factors in the index and save some space.
  417. [source,console]
  418. ----
  419. PUT my-index-000001
  420. {
  421. "mappings": {
  422. "dynamic_templates": [
  423. {
  424. "strings_as_keywords": {
  425. "match_mapping_type": "string",
  426. "mapping": {
  427. "type": "text",
  428. "norms": false,
  429. "fields": {
  430. "keyword": {
  431. "type": "keyword",
  432. "ignore_above": 256
  433. }
  434. }
  435. }
  436. }
  437. }
  438. ]
  439. }
  440. }
  441. ----
  442. The sub `keyword` field appears in this template to be consistent with the
  443. default rules of dynamic mappings. Of course if you do not need them because
  444. you don't need to perform exact search or aggregate on this field, you could
  445. remove it as described in the previous section.
  446. ===== Time series
  447. When doing time series analysis with Elasticsearch, it is common to have many
  448. numeric fields that you will often aggregate on but never filter on. In such a
  449. case, you could disable indexing on those fields to save disk space and also
  450. maybe gain some indexing speed:
  451. [source,console]
  452. ----
  453. PUT my-index-000001
  454. {
  455. "mappings": {
  456. "dynamic_templates": [
  457. {
  458. "unindexed_longs": {
  459. "match_mapping_type": "long",
  460. "mapping": {
  461. "type": "long",
  462. "index": false
  463. }
  464. }
  465. },
  466. {
  467. "unindexed_doubles": {
  468. "match_mapping_type": "double",
  469. "mapping": {
  470. "type": "float", <1>
  471. "index": false
  472. }
  473. }
  474. }
  475. ]
  476. }
  477. }
  478. ----
  479. <1> Like the default dynamic mapping rules, doubles are mapped as floats, which
  480. are usually accurate enough, yet require half the disk space.