disk-usage.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. === Watch your shard size
  125. Larger shards are going to be more efficient at storing data. To increase the size of your shards, you can decrease the number of primary shards in an index by <<indices-create-index,creating indices>> with less primary shards, creating less indices (e.g. by leveraging the <<indices-rollover-index,Rollover API>>), or modifying an existing index using the <<indices-shrink-index,Shrink API>>.
  126. Keep in mind that large shard sizes come with drawbacks, such as long full recovery times.
  127. [float]
  128. === Disable `_source`
  129. The <<mapping-source-field,`_source`>> field stores the original JSON body of the document. If you don’t need access to it you can disable it. However, APIs that needs access to `_source` such as update and reindex won’t work.
  130. [float]
  131. === Use `best_compression`
  132. The `_source` and stored fields can easily take a non negligible amount of disk
  133. space. They can be compressed more aggressively by using the `best_compression`
  134. <<index-codec,codec>>.
  135. [float]
  136. === Force Merge
  137. Indices in Elasticsearch are stored in one or more shards. Each shard is a Lucene index and made up of one or more segments - the actual files on disk. Larger segments are more efficient for storing data.
  138. The <<indices-forcemerge,`_forcemerge` API>> can be used to reduce the number of segments per shard. In many cases, the number of segments can be reduced to one per shard by setting `max_num_segments=1`.
  139. [float]
  140. === Shrink Index
  141. The <<indices-shrink-index,Shrink API>> allows you to reduce the number of shards in an index. Together with the Force Merge API above, this can significantly reduce the number of shards and segments of an index.
  142. [float]
  143. === Use the smallest numeric type that is sufficient
  144. The type that you pick for <<number,numeric data>> can have a significant impact
  145. on disk usage. In particular, integers should be stored using an integer type
  146. (`byte`, `short`, `integer` or `long`) and floating points should either be
  147. stored in a `scaled_float` if appropriate or in the smallest type that fits the
  148. use-case: using `float` over `double`, or `half_float` over `float` will help
  149. save storage.
  150. [float]
  151. === Use index sorting to colocate similar documents
  152. When Elasticsearch stores `_source`, it compresses multiple documents at once
  153. in order to improve the overall compression ratio. For instance it is very
  154. common that documents share the same field names, and quite common that they
  155. share some field values, especially on fields that have a low cardinality or
  156. a https://en.wikipedia.org/wiki/Zipf%27s_law[zipfian] distribution.
  157. By default documents are compressed together in the order that they are added
  158. to the index. If you enabled <<index-modules-index-sorting,index sorting>>
  159. then instead they are compressed in sorted order. Sorting documents with similar
  160. structure, fields, and values together should improve the compression ratio.
  161. [float]
  162. === Put fields in the same order in documents
  163. Due to the fact that multiple documents are compressed together into blocks,
  164. it is more likely to find longer duplicate strings in those `_source` documents
  165. if fields always occur in the same order.