source-field.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 my-index-000001
  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 <<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. [[include-exclude]]
  42. ==== Including / Excluding fields from `_source`
  43. An expert-only feature is the ability to prune the contents of the `_source`
  44. field after the document has been indexed, but before the `_source` field is
  45. stored.
  46. WARNING: Removing fields from the `_source` has similar downsides to disabling
  47. `_source`, especially the fact that you cannot reindex documents from one
  48. Elasticsearch index to another. Consider using
  49. <<source-filtering,source filtering>> instead.
  50. The `includes`/`excludes` parameters (which also accept wildcards) can be used
  51. as follows:
  52. [source,console]
  53. --------------------------------------------------
  54. PUT logs
  55. {
  56. "mappings": {
  57. "_source": {
  58. "includes": [
  59. "*.count",
  60. "meta.*"
  61. ],
  62. "excludes": [
  63. "meta.description",
  64. "meta.other.*"
  65. ]
  66. }
  67. }
  68. }
  69. PUT logs/_doc/1
  70. {
  71. "requests": {
  72. "count": 10,
  73. "foo": "bar" <1>
  74. },
  75. "meta": {
  76. "name": "Some metric",
  77. "description": "Some metric description", <1>
  78. "other": {
  79. "foo": "one", <1>
  80. "baz": "two" <1>
  81. }
  82. }
  83. }
  84. GET logs/_search
  85. {
  86. "query": {
  87. "match": {
  88. "meta.other.foo": "one" <2>
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. <1> These fields will be removed from the stored `_source` field.
  94. <2> We can still search on this field, even though it is not in the stored `_source`.