templates.asciidoc 19 KB

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