source-field.asciidoc 3.7 KB

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