synthetic-source.asciidoc 3.2 KB

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