templates.asciidoc 15 KB

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