templates.asciidoc 18 KB

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