synthetic-source.asciidoc 10 KB

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