source-field.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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,console]
  12. --------------------------------------------------
  13. PUT tweets
  14. {
  15. "mappings": {
  16. "_source": {
  17. "enabled": false
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. [WARNING]
  23. .Think before disabling the `_source` field
  24. ==================================================
  25. Users often disable the `_source` field without thinking about the
  26. consequences, and then live to regret it. If the `_source` field isn't
  27. available then a number of features are not supported:
  28. * The <<docs-update,`update`>>, <<docs-update-by-query,`update_by_query`>>,
  29. and <<docs-reindex,`reindex`>> APIs.
  30. * On the fly <<request-body-search-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. **************************************************
  51. [[include-exclude]]
  52. ==== Including / Excluding fields from `_source`
  53. An expert-only feature is the ability to prune the contents of the `_source`
  54. field after the document has been indexed, but before the `_source` field is
  55. stored.
  56. WARNING: Removing fields from the `_source` has similar downsides to disabling
  57. `_source`, especially the fact that you cannot reindex documents from one
  58. Elasticsearch index to another. Consider using
  59. <<source-filtering,source filtering>> instead.
  60. The `includes`/`excludes` parameters (which also accept wildcards) can be used
  61. as follows:
  62. [source,console]
  63. --------------------------------------------------
  64. PUT logs
  65. {
  66. "mappings": {
  67. "_source": {
  68. "includes": [
  69. "*.count",
  70. "meta.*"
  71. ],
  72. "excludes": [
  73. "meta.description",
  74. "meta.other.*"
  75. ]
  76. }
  77. }
  78. }
  79. PUT logs/_doc/1
  80. {
  81. "requests": {
  82. "count": 10,
  83. "foo": "bar" <1>
  84. },
  85. "meta": {
  86. "name": "Some metric",
  87. "description": "Some metric description", <1>
  88. "other": {
  89. "foo": "one", <1>
  90. "baz": "two" <1>
  91. }
  92. }
  93. }
  94. GET logs/_search
  95. {
  96. "query": {
  97. "match": {
  98. "meta.other.foo": "one" <2>
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. <1> These fields will be removed from the stored `_source` field.
  104. <2> We can still search on this field, even though it is not in the stored `_source`.