synthetic-source.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. [[synthetic-source]]
  2. ==== Synthetic `_source` preview:[]
  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. Enable this by setting `mode: synthetic` in `_source`:
  7. [source,console,id=enable-synthetic-source-example]
  8. ----
  9. PUT idx
  10. {
  11. "mappings": {
  12. "_source": {
  13. "mode": "synthetic"
  14. }
  15. }
  16. }
  17. ----
  18. // TESTSETUP
  19. While this on the fly reconstruction is *generally* slower than saving the source
  20. documents verbatim and loading them at query time, it saves a lot of storage
  21. space. There are a couple of restrictions to be aware of:
  22. * When you retrieve synthetic `_source` content it undergoes minor
  23. <<synthetic-source-modifications,modifications>> compared to the original JSON.
  24. * Synthetic `_source` can be used with indices that contain only these field
  25. types:
  26. ** <<aggregate-metric-double-synthetic-source, `aggregate_metric_double`>>
  27. ** <<boolean-synthetic-source,`boolean`>>
  28. ** <<numeric-synthetic-source,`byte`>>
  29. ** <<date-synthetic-source,`date`>>
  30. ** <<date-nanos-synthetic-source,`date_nanos`>>
  31. ** <<dense-vector-synthetic-source,`dense_vector`>>
  32. ** <<numeric-synthetic-source,`double`>>
  33. ** <<numeric-synthetic-source,`float`>>
  34. ** <<geo-point-synthetic-source,`geo_point`>>
  35. ** <<numeric-synthetic-source,`half_float`>>
  36. ** <<histogram-synthetic-source,`histogram`>>
  37. ** <<numeric-synthetic-source,`integer`>>
  38. ** <<ip-synthetic-source,`ip`>>
  39. ** <<keyword-synthetic-source,`keyword`>>
  40. ** <<numeric-synthetic-source,`long`>>
  41. ** <<numeric-synthetic-source,`scaled_float`>>
  42. ** <<numeric-synthetic-source,`short`>>
  43. ** <<text-synthetic-source,`text`>>
  44. ** <<version-synthetic-source,`version`>>
  45. Runtime fields cannot, at this stage, use synthetic `_source`.
  46. [[synthetic-source-modifications]]
  47. ===== Synthetic `_source` modifications
  48. When synthetic `_source` is enabled, retrieved documents undergo some
  49. modifications compared to the original JSON.
  50. [[synthetic-source-modifications-leaf-arrays]]
  51. ====== Arrays moved to leaf fields
  52. Synthetic `_source` arrays are moved to leaves. For example:
  53. [source,console,id=synthetic-source-leaf-arrays-example]
  54. ----
  55. PUT idx/_doc/1
  56. {
  57. "foo": [
  58. {
  59. "bar": 1
  60. },
  61. {
  62. "bar": 2
  63. }
  64. ]
  65. }
  66. ----
  67. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  68. Will become:
  69. [source,console-result]
  70. ----
  71. {
  72. "foo": {
  73. "bar": [1, 2]
  74. }
  75. }
  76. ----
  77. // TEST[s/^/{"_source":/ s/\n$/}/]
  78. [[synthetic-source-modifications-field-names]]
  79. ====== Fields named as they are mapped
  80. Synthetic source names fields as they are named in the mapping. When used
  81. with <<dynamic,dynamic mapping>>, fields with dots (`.`) in their names are, by
  82. default, interpreted as multiple objects, while dots in field names are
  83. preserved within objects that have <<subobjects>> disabled. For example:
  84. [source,console,id=synthetic-source-objecty-example]
  85. ----
  86. PUT idx/_doc/1
  87. {
  88. "foo.bar.baz": 1
  89. }
  90. ----
  91. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  92. Will become:
  93. [source,console-result]
  94. ----
  95. {
  96. "foo": {
  97. "bar": {
  98. "baz": 1
  99. }
  100. }
  101. }
  102. ----
  103. // TEST[s/^/{"_source":/ s/\n$/}/]
  104. [[synthetic-source-modifications-alphabetical]]
  105. ====== Alphabetical sorting
  106. Synthetic `_source` fields are sorted alphabetically. The
  107. https://www.rfc-editor.org/rfc/rfc7159.html[JSON RFC] defines objects as
  108. "an unordered collection of zero or more name/value pairs" so applications
  109. shouldn't care but without synthetic `_source` the original ordering is
  110. preserved and some applications may, counter to the spec, do something with
  111. that ordering.