synthetic-source.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. ** <<dense-vector-synthetic-source,`dense_vector`>>
  30. ** <<numeric-synthetic-source,`double`>>
  31. ** <<numeric-synthetic-source,`float`>>
  32. ** <<geo-point-synthetic-source,`geo_point`>>
  33. ** <<numeric-synthetic-source,`half_float`>>
  34. ** <<histogram-synthetic-source,`histogram`>>
  35. ** <<numeric-synthetic-source,`integer`>>
  36. ** <<ip-synthetic-source,`ip`>>
  37. ** <<keyword-synthetic-source,`keyword`>>
  38. ** <<numeric-synthetic-source,`long`>>
  39. ** <<numeric-synthetic-source,`scaled_float`>>
  40. ** <<numeric-synthetic-source,`short`>>
  41. ** <<text-synthetic-source,`text`>> (with a `keyword` sub-field)
  42. ** <<version-synthetic-source,`version`>>
  43. Runtime fields cannot, at this stage, use synthetic `_source`.
  44. [[synthetic-source-modifications]]
  45. ===== Synthetic source modifications
  46. When synthetic `_source` is enabled, retrieved documents undergo some
  47. modifications compared to the original JSON.
  48. [[synthetic-source-modifications-leaf-arrays]]
  49. ====== Arrays moved to leaf fields
  50. Synthetic `_source` arrays are moved to leaves. For example:
  51. [source,console,id=synthetic-source-leaf-arrays-example]
  52. ----
  53. PUT idx/_doc/1
  54. {
  55. "foo": [
  56. {
  57. "bar": 1
  58. },
  59. {
  60. "bar": 2
  61. }
  62. ]
  63. }
  64. ----
  65. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  66. Will become:
  67. [source,console-result]
  68. ----
  69. {
  70. "foo": {
  71. "bar": [1, 2]
  72. }
  73. }
  74. ----
  75. // TEST[s/^/{"_source":/ s/\n$/}/]
  76. [[synthetic-source-modifications-field-names]]
  77. ====== Fields named as they are mapped
  78. Synthetic source names fields as they are named in the mapping. When used
  79. with <<dynamic,dynamic mapping>>, fields with dots (`.`) in their names are, by
  80. default, interpreted as multiple objects, while dots in field names are
  81. preserved within objects that have <<subobjects>> disabled. For example:
  82. [source,console,id=synthetic-source-objecty-example]
  83. ----
  84. PUT idx/_doc/1
  85. {
  86. "foo.bar.baz": 1
  87. }
  88. ----
  89. // TEST[s/$/\nGET idx\/_doc\/1?filter_path=_source\n/]
  90. Will become:
  91. [source,console-result]
  92. ----
  93. {
  94. "foo": {
  95. "bar": {
  96. "baz": 1
  97. }
  98. }
  99. }
  100. ----
  101. // TEST[s/^/{"_source":/ s/\n$/}/]
  102. [[synthetic-source-modifications-alphabetical]]
  103. ====== Alphabetical sorting
  104. Synthetic `_source` fields are sorted alphabetically. The
  105. https://www.rfc-editor.org/rfc/rfc7159.html[JSON RFC] defines objects as
  106. "an unordered collection of zero or more name/value pairs" so applications
  107. shouldn't care but without synthetic `_source` the original ordering is
  108. preserved and some applications may, counter to the spec, do something with
  109. that ordering.