disk-usage.asciidoc 6.3 KB

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