disk-usage.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. [[tune-for-disk-usage]]
  2. == Tune for disk usage
  3. [float]
  4. === Disable the features you do not need
  5. By default elasticsearch indexes and adds doc values to most fields so that they
  6. can be searched and aggregated out of the box. For instance if you have a numeric
  7. field called `foo` that you need to run histograms on but that you never need to
  8. filter on, you can safely disable indexing on this field in your
  9. <<mappings,mappings>>:
  10. [source,js]
  11. --------------------------------------------------
  12. PUT index
  13. {
  14. "mappings": {
  15. "type": {
  16. "properties": {
  17. "foo": {
  18. "type": "integer",
  19. "index": false
  20. }
  21. }
  22. }
  23. }
  24. }
  25. --------------------------------------------------
  26. // CONSOLE
  27. <<text,`text`>> fields store normalization factors in the index in order to be
  28. able to score documents. If you only need matching capabilities on a `text`
  29. field but do not care about the produced scores, you can configure elasticsearch
  30. to not write norms to the index:
  31. [source,js]
  32. --------------------------------------------------
  33. PUT index
  34. {
  35. "mappings": {
  36. "type": {
  37. "properties": {
  38. "foo": {
  39. "type": "text",
  40. "norms": false
  41. }
  42. }
  43. }
  44. }
  45. }
  46. --------------------------------------------------
  47. // CONSOLE
  48. <<text,`text`>> fields also store frequencies and positions in the index by
  49. default. Frequencies are used to compute scores and positions are used to run
  50. phrase queries. If you do not need to run phrase queries, you can tell
  51. elasticsearch to not index positions:
  52. [source,js]
  53. --------------------------------------------------
  54. PUT index
  55. {
  56. "mappings": {
  57. "type": {
  58. "properties": {
  59. "foo": {
  60. "type": "text",
  61. "index_options": "freqs"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. // CONSOLE
  69. Furthermore if you do not care about scoring either, you can configure
  70. elasticsearch to just index matching documents for every term. You will
  71. still be able to search on this field, but phrase queries will raise errors
  72. and scoring will assume that terms appear only once in every document.
  73. [source,js]
  74. --------------------------------------------------
  75. PUT index
  76. {
  77. "mappings": {
  78. "type": {
  79. "properties": {
  80. "foo": {
  81. "type": "text",
  82. "norms": false,
  83. "index_options": "freqs"
  84. }
  85. }
  86. }
  87. }
  88. }
  89. --------------------------------------------------
  90. // CONSOLE
  91. [float]
  92. === Don't use default dynamic string mappings
  93. The default <<dynamic-mapping,dynamic string mappings>> will index string fields
  94. both as <<text,`text`>> and <<keyword,`keyword`>>. This is wasteful if you only
  95. need one of them. Typically an `id` field will only need to be indexed as a
  96. `keyword` while a `body` field will only need to be indexed as a `text` field.
  97. This can be disabled by either configuring explicit mappings on string fields
  98. or setting up dynamic templates that will map string fields as either `text`
  99. or `keyword`.
  100. For instance, here is a template that can be used in order to only map string
  101. fields as `keyword`:
  102. [source,js]
  103. --------------------------------------------------
  104. PUT index
  105. {
  106. "mappings": {
  107. "type": {
  108. "dynamic_templates": [
  109. {
  110. "strings": {
  111. "match_mapping_type": "string",
  112. "mapping": {
  113. "type": "keyword"
  114. }
  115. }
  116. }
  117. ]
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // CONSOLE
  123. [float]
  124. === Disable `_all`
  125. The <<mapping-all-field,`_all`>> field indexes the value of all fields of a
  126. document and can use significant space. If you never need to search against all
  127. fields at the same time, it can be disabled.
  128. [float]
  129. === Use `best_compression`
  130. The `_source` and stored fields can easily take a non negligible amount of disk
  131. space. They can be compressed more aggressively by using the `best_compression`
  132. <<index-codec,codec>>.
  133. [float]
  134. === Use the smallest numeric type that is sufficient
  135. The type that you pick for <<number,numeric data>> can have a significant impact
  136. on disk usage. In particular, integers should be stored using an integer type
  137. (`byte`, `short`, `integer` or `long`) and floating points should either be
  138. stored in a `scaled_float` if appropriate or in the smallest type that fits the
  139. use-case: using `float` over `double`, or `half_float` over `float` will help
  140. save storage.
  141. [float]
  142. === Use index sorting to colocate similar documents
  143. When Elasticsearch stores `_source`, it compresses multiple documents at once
  144. in order to improve the overall compression ratio. For instance it is very
  145. common that documents share the same field names, and quite common that they
  146. share some field values, especially on fields that have a low cardinality or
  147. a https://en.wikipedia.org/wiki/Zipf%27s_law[zipfian] distribution.
  148. By default documents are compressed together in the order that they are added
  149. to the index. If you enabled <<index-modules-index-sorting,index sorting>>
  150. then instead they are compressed in sorted order. Sorting documents with similar
  151. structure, fields, and values together should improve the compression ratio.
  152. [float]
  153. === Put fields in the same order in documents
  154. Due to the fact that multiple documents are compressed together into blocks,
  155. it is more likely to find longer duplicate strings in those `_source` documents
  156. if fields always occur in the same order.