synthetic-source.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. [[synthetic-source]]
  2. ==== Synthetic `_source`
  3. Though very handy to have around, the source field takes up a significant amount
  4. of space on disk. Instead of storing source documents on disk exactly as you
  5. send them, Elasticsearch can reconstruct source content on the fly upon retrieval.
  6. To enable this https://www.elastic.co/subscriptions[subscription] feature, use the value `synthetic` for the index setting `index.mapping.source.mode`:
  7. [source,console,id=enable-synthetic-source-example]
  8. ----
  9. PUT idx
  10. {
  11. "settings": {
  12. "index": {
  13. "mapping": {
  14. "source": {
  15. "mode": "synthetic"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. ----
  22. // TESTSETUP
  23. While this on-the-fly reconstruction is _generally_ slower than saving the source
  24. documents verbatim and loading them at query time, it saves a lot of storage
  25. space. Additional latency can be avoided by not loading `_source` field in queries when it is not needed.
  26. [[synthetic-source-fields]]
  27. ===== Supported fields
  28. Synthetic `_source` is supported by all field types. Depending on implementation details, field types have different
  29. properties when used with synthetic `_source`.
  30. <<synthetic-source-fields-native-list, Most field types>> construct synthetic `_source` using existing data, most
  31. commonly <<doc-values,`doc_values`>> and <<stored-fields, stored fields>>. For these field types, no additional space
  32. is needed to store the contents of `_source` field. Due to the storage layout of <<doc-values,`doc_values`>>, the
  33. generated `_source` field undergoes <<synthetic-source-modifications, modifications>> compared to the original document.
  34. For all other field types, the original value of the field is stored as is, in the same way as the `_source` field in
  35. non-synthetic mode. In this case there are no modifications and field data in `_source` is the same as in the original
  36. document. Similarly, malformed values of fields that use <<ignore-malformed,`ignore_malformed`>> or
  37. <<ignore-above,`ignore_above`>> need to be stored as is. This approach is less storage efficient since data needed for
  38. `_source` reconstruction is stored in addition to other data required to index the field (like `doc_values`).
  39. [[synthetic-source-restrictions]]
  40. ===== Synthetic `_source` restrictions
  41. Some field types have additional restrictions. These restrictions are documented in the **synthetic `_source`** section
  42. of the field type's <<mapping-types,documentation>>.
  43. [[synthetic-source-modifications]]
  44. ===== Synthetic `_source` modifications
  45. When synthetic `_source` is enabled, retrieved documents undergo some
  46. modifications compared to the original JSON.
  47. [[synthetic-source-modifications-leaf-arrays]]
  48. ====== Arrays moved to leaf fields
  49. Synthetic `_source` arrays are moved to leaves. For example:
  50. [source,console,id=synthetic-source-leaf-arrays-example]
  51. ----
  52. PUT idx/_doc/1
  53. {
  54. "foo": [
  55. {
  56. "bar": 1
  57. },
  58. {
  59. "bar": 2
  60. }
  61. ]
  62. }
  63. ----
  64. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  65. Will become:
  66. [source,console-result]
  67. ----
  68. {
  69. "foo": {
  70. "bar": [1, 2]
  71. }
  72. }
  73. ----
  74. // TEST[s/^/{"_source":/ s/\n$/}/]
  75. This can cause some arrays to vanish:
  76. [source,console,id=synthetic-source-leaf-arrays-example-sneaky]
  77. ----
  78. PUT idx/_doc/1
  79. {
  80. "foo": [
  81. {
  82. "bar": 1
  83. },
  84. {
  85. "baz": 2
  86. }
  87. ]
  88. }
  89. ----
  90. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  91. Will become:
  92. [source,console-result]
  93. ----
  94. {
  95. "foo": {
  96. "bar": 1,
  97. "baz": 2
  98. }
  99. }
  100. ----
  101. // TEST[s/^/{"_source":/ s/\n$/}/]
  102. [[synthetic-source-modifications-field-names]]
  103. ====== Fields named as they are mapped
  104. Synthetic source names fields as they are named in the mapping. When used
  105. with <<dynamic,dynamic mapping>>, fields with dots (`.`) in their names are, by
  106. default, interpreted as multiple objects, while dots in field names are
  107. preserved within objects that have <<subobjects>> disabled. For example:
  108. [source,console,id=synthetic-source-objecty-example]
  109. ----
  110. PUT idx/_doc/1
  111. {
  112. "foo.bar.baz": 1
  113. }
  114. ----
  115. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  116. Will become:
  117. [source,console-result]
  118. ----
  119. {
  120. "foo": {
  121. "bar": {
  122. "baz": 1
  123. }
  124. }
  125. }
  126. ----
  127. // TEST[s/^/{"_source":/ s/\n$/}/]
  128. This impacts how source contents can be referenced in <<modules-scripting-using,scripts>>. For instance, referencing
  129. a script in its original source form will return null:
  130. [source,js]
  131. ----
  132. "script": { "source": """ emit(params._source['foo.bar.baz']) """ }
  133. ----
  134. // NOTCONSOLE
  135. Instead, source references need to be in line with the mapping structure:
  136. [source,js]
  137. ----
  138. "script": { "source": """ emit(params._source['foo']['bar']['baz']) """ }
  139. ----
  140. // NOTCONSOLE
  141. or simply
  142. [source,js]
  143. ----
  144. "script": { "source": """ emit(params._source.foo.bar.baz) """ }
  145. ----
  146. // NOTCONSOLE
  147. The following <<modules-scripting-fields, field APIs>> are preferable as, in addition to being agnostic to the
  148. mapping structure, they make use of docvalues if available and fall back to synthetic source only when needed. This
  149. reduces source synthesizing, a slow and costly operation.
  150. [source,js]
  151. ----
  152. "script": { "source": """ emit(field('foo.bar.baz').get(null)) """ }
  153. "script": { "source": """ emit($('foo.bar.baz', null)) """ }
  154. ----
  155. // NOTCONSOLE
  156. [[synthetic-source-modifications-alphabetical]]
  157. ====== Alphabetical sorting
  158. Synthetic `_source` fields are sorted alphabetically. The
  159. https://www.rfc-editor.org/rfc/rfc7159.html[JSON RFC] defines objects as
  160. "an unordered collection of zero or more name/value pairs" so applications
  161. shouldn't care but without synthetic `_source` the original ordering is
  162. preserved and some applications may, counter to the spec, do something with
  163. that ordering.
  164. [[synthetic-source-modifications-ranges]]
  165. ====== Representation of ranges
  166. Range field values (e.g. `long_range`) are always represented as inclusive on both sides with bounds adjusted
  167. accordingly. See <<range-synthetic-source-inclusive, examples>>.
  168. [[synthetic-source-precision-loss-for-point-types]]
  169. ====== Reduced precision of `geo_point` values
  170. Values of `geo_point` fields are represented in synthetic `_source` with reduced precision. See
  171. <<geo-point-synthetic-source, examples>>.
  172. [[synthetic-source-keep]]
  173. ====== Minimizing source modifications
  174. It is possible to avoid synthetic source modifications for a particular object or field, at extra storage cost.
  175. This is controlled through param `synthetic_source_keep` with the following option:
  176. - `none`: synthetic source diverges from the original source as described above (default).
  177. - `arrays`: arrays of the corresponding field or object preserve the original element ordering and duplicate elements.
  178. The synthetic source fragment for such arrays is not guaranteed to match the original source exactly, e.g. array
  179. `[1, 2, [5], [[4, [3]]], 5]` may appear as-is or in an equivalent format like `[1, 2, 5, 4, 3, 5]`. The exact format
  180. may change in the future, in an effort to reduce the storage overhead of this option.
  181. - `all`: the source for both singleton instances and arrays of the corresponding field or object gets recorded. When
  182. applied to objects, the source of all sub-objects and sub-fields gets captured. Furthermore, the original source of
  183. arrays gets captured and appears in synthetic source with no modifications.
  184. For instance:
  185. [source,console,id=create-index-with-synthetic-source-keep]
  186. ----
  187. PUT idx_keep
  188. {
  189. "settings": {
  190. "index": {
  191. "mapping": {
  192. "source": {
  193. "mode": "synthetic"
  194. }
  195. }
  196. }
  197. },
  198. "mappings": {
  199. "properties": {
  200. "path": {
  201. "type": "object",
  202. "synthetic_source_keep": "all"
  203. },
  204. "ids": {
  205. "type": "integer",
  206. "synthetic_source_keep": "arrays"
  207. }
  208. }
  209. }
  210. }
  211. ----
  212. // TEST
  213. [source,console,id=synthetic-source-keep-example]
  214. ----
  215. PUT idx_keep/_doc/1
  216. {
  217. "path": {
  218. "to": [
  219. { "foo": [3, 2, 1] },
  220. { "foo": [30, 20, 10] }
  221. ],
  222. "bar": "baz"
  223. },
  224. "ids": [ 200, 100, 300, 100 ]
  225. }
  226. ----
  227. // TEST[s/$/\nGET idx_keep\/_doc\/1?filter_path=_source\n/]
  228. returns the original source, with no array deduplication and sorting:
  229. [source,console-result]
  230. ----
  231. {
  232. "path": {
  233. "to": [
  234. { "foo": [3, 2, 1] },
  235. { "foo": [30, 20, 10] }
  236. ],
  237. "bar": "baz"
  238. },
  239. "ids": [ 200, 100, 300, 100 ]
  240. }
  241. ----
  242. // TEST[s/^/{"_source":/ s/\n$/}/]
  243. The option for capturing the source of arrays can be applied at index level, by setting
  244. `index.mapping.synthetic_source_keep` to `arrays`. This applies to all objects and fields in the index, except for
  245. the ones with explicit overrides of `synthetic_source_keep` set to `none`. In this case, the storage overhead grows
  246. with the number and sizes of arrays present in source of each document, naturally.
  247. [[synthetic-source-fields-native-list]]
  248. ===== Field types that support synthetic source with no storage overhead
  249. The following field types support synthetic source using data from <<doc-values,`doc_values`>> or
  250. <stored-fields, stored fields>>, and require no additional storage space to construct the `_source` field.
  251. NOTE: If you enable the <<ignore-malformed,`ignore_malformed`>> or <<ignore-above,`ignore_above`>> settings, then
  252. additional storage is required to store ignored field values for these types.
  253. ** <<aggregate-metric-double-synthetic-source, `aggregate_metric_double`>>
  254. ** {plugins}/mapper-annotated-text-usage.html#annotated-text-synthetic-source[`annotated-text`]
  255. ** <<binary-synthetic-source,`binary`>>
  256. ** <<boolean-synthetic-source,`boolean`>>
  257. ** <<numeric-synthetic-source,`byte`>>
  258. ** <<date-synthetic-source,`date`>>
  259. ** <<date-nanos-synthetic-source,`date_nanos`>>
  260. ** <<dense-vector-synthetic-source,`dense_vector`>>
  261. ** <<numeric-synthetic-source,`double`>>
  262. ** <<flattened-synthetic-source, `flattened`>>
  263. ** <<numeric-synthetic-source,`float`>>
  264. ** <<geo-point-synthetic-source,`geo_point`>>
  265. ** <<numeric-synthetic-source,`half_float`>>
  266. ** <<histogram-synthetic-source,`histogram`>>
  267. ** <<numeric-synthetic-source,`integer`>>
  268. ** <<ip-synthetic-source,`ip`>>
  269. ** <<keyword-synthetic-source,`keyword`>>
  270. ** <<numeric-synthetic-source,`long`>>
  271. ** <<range-synthetic-source,`range` types>>
  272. ** <<numeric-synthetic-source,`scaled_float`>>
  273. ** <<numeric-synthetic-source,`short`>>
  274. ** <<text-synthetic-source,`text`>>
  275. ** <<version-synthetic-source,`version`>>
  276. ** <<wildcard-synthetic-source,`wildcard`>>