synthetic-source.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ** <<wildcard-synthetic-source,`wildcard`>>
  46. Runtime fields cannot, at this stage, use synthetic `_source`.
  47. [[synthetic-source-modifications]]
  48. ===== Synthetic `_source` modifications
  49. When synthetic `_source` is enabled, retrieved documents undergo some
  50. modifications compared to the original JSON.
  51. [[synthetic-source-modifications-leaf-arrays]]
  52. ====== Arrays moved to leaf fields
  53. Synthetic `_source` arrays are moved to leaves. For example:
  54. [source,console,id=synthetic-source-leaf-arrays-example]
  55. ----
  56. PUT idx/_doc/1
  57. {
  58. "foo": [
  59. {
  60. "bar": 1
  61. },
  62. {
  63. "bar": 2
  64. }
  65. ]
  66. }
  67. ----
  68. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  69. Will become:
  70. [source,console-result]
  71. ----
  72. {
  73. "foo": {
  74. "bar": [1, 2]
  75. }
  76. }
  77. ----
  78. // TEST[s/^/{"_source":/ s/\n$/}/]
  79. [[synthetic-source-modifications-field-names]]
  80. ====== Fields named as they are mapped
  81. Synthetic source names fields as they are named in the mapping. When used
  82. with <<dynamic,dynamic mapping>>, fields with dots (`.`) in their names are, by
  83. default, interpreted as multiple objects, while dots in field names are
  84. preserved within objects that have <<subobjects>> disabled. For example:
  85. [source,console,id=synthetic-source-objecty-example]
  86. ----
  87. PUT idx/_doc/1
  88. {
  89. "foo.bar.baz": 1
  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": {
  99. "baz": 1
  100. }
  101. }
  102. }
  103. ----
  104. // TEST[s/^/{"_source":/ s/\n$/}/]
  105. [[synthetic-source-modifications-alphabetical]]
  106. ====== Alphabetical sorting
  107. Synthetic `_source` fields are sorted alphabetically. The
  108. https://www.rfc-editor.org/rfc/rfc7159.html[JSON RFC] defines objects as
  109. "an unordered collection of zero or more name/value pairs" so applications
  110. shouldn't care but without synthetic `_source` the original ordering is
  111. preserved and some applications may, counter to the spec, do something with
  112. that ordering.