source-field.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. [[mapping-source-field]]
  2. === `_source` field
  3. The `_source` field contains the original JSON document body that was passed
  4. at index time. The `_source` field itself is not indexed (and thus is not
  5. searchable), but it is stored so that it can be returned when executing
  6. _fetch_ requests, like <<docs-get,get>> or <<search-search,search>>.
  7. ==== Disabling the `_source` field
  8. Though very handy to have around, the source field does incur storage overhead
  9. within the index. For this reason, it can be disabled as follows:
  10. [source,js]
  11. --------------------------------------------------
  12. PUT tweets
  13. {
  14. "mappings": {},
  15. "tweet": {
  16. "_source": {
  17. "enabled": false
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. // AUTOSENSE
  23. [WARNING]
  24. .Think before disabling the `_source` field
  25. ==================================================
  26. Users often disable the `_source` field without thinking about the
  27. consequences, and then live to regret it. If the `_source` field isn't
  28. available then a number of features are not supported:
  29. * The <<docs-update,`update` API>>.
  30. * On the fly <<search-request-highlighting,highlighting>>.
  31. * The ability to reindex from one Elasticsearch index to another, either
  32. to change mappings or analysis, or to upgrade an index to a new major
  33. version.
  34. * The ability to debug queries or aggregations by viewing the original
  35. document used at index time.
  36. * Potentially in the future, the ability to repair index corruption
  37. automatically.
  38. ==================================================
  39. TIP: If disk space is a concern, rather increase the
  40. <<index-codec,compression level>> instead of disabling the `_source`.
  41. .The metrics use case
  42. **************************************************
  43. The _metrics_ use case is distinct from other time-based or logging use cases
  44. in that there are many small documents which consist only of numbers, dates,
  45. or keywords. There are no updates, no highlighting requests, and the data
  46. ages quickly so there is no need to reindex. Search requests typically use
  47. simple queries to filter the dataset by date or tags, and the results are
  48. returned as aggregations.
  49. In this case, disabling the `_source` field will save space and reduce I/O.
  50. It is also advisable to disable the <<mapping-all-field,`_all` field>> in the
  51. metrics case.
  52. **************************************************
  53. [[include-exclude]]
  54. ==== Including / Excluding fields from `_source`
  55. An expert-only feature is the ability to prune the contents of the `_source`
  56. field after the document has been indexed, but before the `_source` field is
  57. stored.
  58. WARNING: Removing fields from the `_source` has similar downsides to disabling
  59. `_source`, especially the fact that you cannot reindex documents from one
  60. Elasticsearch index to another. Consider using
  61. <<search-request-source-filtering,source filtering>> instead.
  62. The `includes`/`excludes` parameters (which also accept wildcards) can be used
  63. as follows:
  64. [source,js]
  65. --------------------------------------------------
  66. PUT logs
  67. {
  68. "mappings": {
  69. "event": {
  70. "_source": {
  71. "includes": [
  72. "*.count",
  73. "meta.*"
  74. ],
  75. "excludes": [
  76. "meta.description",
  77. "meta.other.*"
  78. ]
  79. }
  80. }
  81. }
  82. }
  83. PUT logs/event/1
  84. {
  85. "requests": {
  86. "count": 10,
  87. "foo": "bar" <1>
  88. },
  89. "meta": {
  90. "name": "Some metric",
  91. "description": "Some metric description", <1>
  92. "other": {
  93. "foo": "one", <1>
  94. "baz": "two" <1>
  95. }
  96. }
  97. }
  98. GET logs/event/_search
  99. {
  100. "query": {
  101. "match": {
  102. "meta.other.foo": "one" <2>
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. // AUTOSENSE
  108. <1> These fields will be removed from the stored `_source` field.
  109. <2> We can still search on this field, even though it is not in the stored `_source`.