disk-usage.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. [[default-dynamic-string-mapping]]
  85. === Don't use default dynamic string mappings
  86. The default <<dynamic-mapping,dynamic string mappings>> will index string fields
  87. both as <<text,`text`>> and <<keyword,`keyword`>>. This is wasteful if you only
  88. need one of them. Typically an `id` field will only need to be indexed as a
  89. `keyword` while a `body` field will only need to be indexed as a `text` field.
  90. This can be disabled by either configuring explicit mappings on string fields
  91. or setting up dynamic templates that will map string fields as either `text`
  92. or `keyword`.
  93. For instance, here is a template that can be used in order to only map string
  94. fields as `keyword`:
  95. [source,js]
  96. --------------------------------------------------
  97. PUT index
  98. {
  99. "mappings": {
  100. "dynamic_templates": [
  101. {
  102. "strings": {
  103. "match_mapping_type": "string",
  104. "mapping": {
  105. "type": "keyword"
  106. }
  107. }
  108. }
  109. ]
  110. }
  111. }
  112. --------------------------------------------------
  113. // CONSOLE
  114. [float]
  115. === Watch your shard size
  116. 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>>.
  117. Keep in mind that large shard sizes come with drawbacks, such as long full recovery times.
  118. [float]
  119. [[disable-source]]
  120. === Disable `_source`
  121. 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.
  122. [float]
  123. [[best-compression]]
  124. === Use `best_compression`
  125. The `_source` and stored fields can easily take a non negligible amount of disk
  126. space. They can be compressed more aggressively by using the `best_compression`
  127. <<index-codec,codec>>.
  128. [float]
  129. === Force Merge
  130. 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.
  131. 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`.
  132. [float]
  133. === Shrink Index
  134. 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.
  135. [float]
  136. === Use the smallest numeric type that is sufficient
  137. The type that you pick for <<number,numeric data>> can have a significant impact
  138. on disk usage. In particular, integers should be stored using an integer type
  139. (`byte`, `short`, `integer` or `long`) and floating points should either be
  140. stored in a `scaled_float` if appropriate or in the smallest type that fits the
  141. use-case: using `float` over `double`, or `half_float` over `float` will help
  142. save storage.
  143. [float]
  144. === Use index sorting to colocate similar documents
  145. When Elasticsearch stores `_source`, it compresses multiple documents at once
  146. in order to improve the overall compression ratio. For instance it is very
  147. common that documents share the same field names, and quite common that they
  148. share some field values, especially on fields that have a low cardinality or
  149. a https://en.wikipedia.org/wiki/Zipf%27s_law[zipfian] distribution.
  150. By default documents are compressed together in the order that they are added
  151. to the index. If you enabled <<index-modules-index-sorting,index sorting>>
  152. then instead they are compressed in sorted order. Sorting documents with similar
  153. structure, fields, and values together should improve the compression ratio.
  154. [float]
  155. === Put fields in the same order in documents
  156. Due to the fact that multiple documents are compressed together into blocks,
  157. it is more likely to find longer duplicate strings in those `_source` documents
  158. if fields always occur in the same order.