synthetic-source.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. [[synthetic-source]]
  2. ==== Synthetic `_source`
  3. IMPORTANT: Synthetic `_source` is Generally Available only for TSDB indices
  4. (indices that have `index.mode` set to `time_series`). For other indices
  5. synthetic `_source` is in technical preview. Features in technical preview may
  6. be changed or removed in a future release. Elastic will apply best effort to fix
  7. any issues, but features in technical preview are not subject to the support SLA
  8. of official GA features.
  9. Though very handy to have around, the source field takes up a significant amount
  10. of space on disk. Instead of storing source documents on disk exactly as you
  11. send them, Elasticsearch can reconstruct source content on the fly upon retrieval.
  12. Enable this by setting `mode: synthetic` in `_source`:
  13. [source,console,id=enable-synthetic-source-example]
  14. ----
  15. PUT idx
  16. {
  17. "mappings": {
  18. "_source": {
  19. "mode": "synthetic"
  20. }
  21. }
  22. }
  23. ----
  24. // TESTSETUP
  25. While this on the fly reconstruction is *generally* slower than saving the source
  26. documents verbatim and loading them at query time, it saves a lot of storage
  27. space. There are a couple of restrictions to be aware of:
  28. * When you retrieve synthetic `_source` content it undergoes minor
  29. <<synthetic-source-modifications,modifications>> compared to the original JSON.
  30. * The `params._source` is unavailable in scripts. Instead use the
  31. {painless}/painless-field-context.html[`doc`] API or the <<script-fields-api, `field`>>.
  32. * Synthetic `_source` can be used with indices that contain only these field
  33. types:
  34. ** <<aggregate-metric-double-synthetic-source, `aggregate_metric_double`>>
  35. ** <<boolean-synthetic-source,`boolean`>>
  36. ** <<numeric-synthetic-source,`byte`>>
  37. ** <<date-synthetic-source,`date`>>
  38. ** <<date-nanos-synthetic-source,`date_nanos`>>
  39. ** <<dense-vector-synthetic-source,`dense_vector`>>
  40. ** <<numeric-synthetic-source,`double`>>
  41. ** <<flattened-synthetic-source, `flattened`>>
  42. ** <<numeric-synthetic-source,`float`>>
  43. ** <<geo-point-synthetic-source,`geo_point`>>
  44. ** <<numeric-synthetic-source,`half_float`>>
  45. ** <<histogram-synthetic-source,`histogram`>>
  46. ** <<numeric-synthetic-source,`integer`>>
  47. ** <<ip-synthetic-source,`ip`>>
  48. ** <<keyword-synthetic-source,`keyword`>>
  49. ** <<numeric-synthetic-source,`long`>>
  50. ** <<numeric-synthetic-source,`scaled_float`>>
  51. ** <<numeric-synthetic-source,`short`>>
  52. ** <<text-synthetic-source,`text`>>
  53. ** <<version-synthetic-source,`version`>>
  54. ** <<wildcard-synthetic-source,`wildcard`>>
  55. Runtime fields cannot, at this stage, use synthetic `_source`.
  56. [[synthetic-source-modifications]]
  57. ===== Synthetic `_source` modifications
  58. When synthetic `_source` is enabled, retrieved documents undergo some
  59. modifications compared to the original JSON.
  60. [[synthetic-source-modifications-leaf-arrays]]
  61. ====== Arrays moved to leaf fields
  62. Synthetic `_source` arrays are moved to leaves. For example:
  63. [source,console,id=synthetic-source-leaf-arrays-example]
  64. ----
  65. PUT idx/_doc/1
  66. {
  67. "foo": [
  68. {
  69. "bar": 1
  70. },
  71. {
  72. "bar": 2
  73. }
  74. ]
  75. }
  76. ----
  77. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  78. Will become:
  79. [source,console-result]
  80. ----
  81. {
  82. "foo": {
  83. "bar": [1, 2]
  84. }
  85. }
  86. ----
  87. // TEST[s/^/{"_source":/ s/\n$/}/]
  88. This can cause some arrays to vanish:
  89. [source,console,id=synthetic-source-leaf-arrays-example-sneaky]
  90. ----
  91. PUT idx/_doc/1
  92. {
  93. "foo": [
  94. {
  95. "bar": 1
  96. },
  97. {
  98. "baz": 2
  99. }
  100. ]
  101. }
  102. ----
  103. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  104. Will become:
  105. [source,console-result]
  106. ----
  107. {
  108. "foo": {
  109. "bar": 1,
  110. "baz": 2
  111. }
  112. }
  113. ----
  114. // TEST[s/^/{"_source":/ s/\n$/}/]
  115. [[synthetic-source-modifications-field-names]]
  116. ====== Fields named as they are mapped
  117. Synthetic source names fields as they are named in the mapping. When used
  118. with <<dynamic,dynamic mapping>>, fields with dots (`.`) in their names are, by
  119. default, interpreted as multiple objects, while dots in field names are
  120. preserved within objects that have <<subobjects>> disabled. For example:
  121. [source,console,id=synthetic-source-objecty-example]
  122. ----
  123. PUT idx/_doc/1
  124. {
  125. "foo.bar.baz": 1
  126. }
  127. ----
  128. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  129. Will become:
  130. [source,console-result]
  131. ----
  132. {
  133. "foo": {
  134. "bar": {
  135. "baz": 1
  136. }
  137. }
  138. }
  139. ----
  140. // TEST[s/^/{"_source":/ s/\n$/}/]
  141. [[synthetic-source-modifications-alphabetical]]
  142. ====== Alphabetical sorting
  143. Synthetic `_source` fields are sorted alphabetically. The
  144. https://www.rfc-editor.org/rfc/rfc7159.html[JSON RFC] defines objects as
  145. "an unordered collection of zero or more name/value pairs" so applications
  146. shouldn't care but without synthetic `_source` the original ordering is
  147. preserved and some applications may, counter to the spec, do something with
  148. that ordering.