source-field.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "_doc": {
  16. "_source": {
  17. "enabled": false
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. [WARNING]
  25. .Think before disabling the `_source` field
  26. ==================================================
  27. Users often disable the `_source` field without thinking about the
  28. consequences, and then live to regret it. If the `_source` field isn't
  29. available then a number of features are not supported:
  30. * The <<docs-update,`update`>>, <<docs-update-by-query,`update_by_query`>>,
  31. and <<docs-reindex,`reindex`>> APIs.
  32. * On the fly <<search-request-highlighting,highlighting>>.
  33. * The ability to reindex from one Elasticsearch index to another, either
  34. to change mappings or analysis, or to upgrade an index to a new major
  35. version.
  36. * The ability to debug queries or aggregations by viewing the original
  37. document used at index time.
  38. * Potentially in the future, the ability to repair index corruption
  39. automatically.
  40. ==================================================
  41. TIP: If disk space is a concern, rather increase the
  42. <<index-codec,compression level>> instead of disabling the `_source`.
  43. .The metrics use case
  44. **************************************************
  45. The _metrics_ use case is distinct from other time-based or logging use cases
  46. in that there are many small documents which consist only of numbers, dates,
  47. or keywords. There are no updates, no highlighting requests, and the data
  48. ages quickly so there is no need to reindex. Search requests typically use
  49. simple queries to filter the dataset by date or tags, and the results are
  50. returned as aggregations.
  51. In this case, disabling the `_source` field will save space and reduce I/O.
  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. "_doc": {
  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/_doc/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/_search
  99. {
  100. "query": {
  101. "match": {
  102. "meta.other.foo": "one" <2>
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. // CONSOLE
  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`.