Reference/How To ...

6. Tune for disk usage

drscg 2018. 10. 5. 18:04

Disable the features you do not need

By default Elasticsearch indexes and adds doc values to most fields so that they can be searched and aggregated out of the box. For instance if you have a numeric field called foo that you need to run histograms on but that you never need to filter on, you can safely disable indexing on this field in your mappings:

기본적으로, Elasticsearch는 대부분의 field가 검색되고 aggregation되도록 index하고 doc values를 추가한다. 예를 들어, histogram을 실행해야 하지만 filtering할 필요가 없는 foo 라는 숫자 field를 가지고 있다면, mapping 에서 이 field의 index르 비활성화할 수 있다.

PUT index
{
  "mappings": {
    "type": {
      "properties": {
        "foo": {
          "type": "integer",
          "index": false
        }
      }
    }
  }
}

text fields store normalization factors in the index in order to be able to score documents. If you only need matching capabilities on a text field but do not care about the produced scores, you can configure Elasticsearch to not write norms to the index:

text field는 document의 score를 계산하기 위해 index에 normalization factor를 저장한다. text field에서 일치(match) 기능은 필요하지만 score에는 관심이 없다면, index에 norms를 쓰지 말라고 설정할 수 있다.

PUT index
{
  "mappings": {
    "type": {
      "properties": {
        "foo": {
          "type": "text",
          "norms": false
        }
      }
    }
  }
}

text fields also store frequencies and positions in the index by default. Frequencies are used to compute scores and positions are used to run phrase queries. If you do not need to run phrase queries, you can tell Elasticsearch to not index positions:

text field는 index에 기본적으로 빈도(frequency)와 위치(position)도 저장한다. 빈도(frequency)는 score를 계산하는데 사용되며, 위치(position)는 phrase query에 사용된다. phrase query가 불필요하다면, 위치(position)를 index하지 않도록 할 수 있다.

PUT index
{
  "mappings": {
    "type": {
      "properties": {
        "foo": {
          "type": "text",
          "index_options": "freqs"
        }
      }
    }
  }
}

Furthermore if you do not care about scoring either, you can configure Elasticsearch to just index matching documents for every term. You will still be able to search on this field, but phrase queries will raise errors and scoring will assume that terms appear only once in every document.

또한, score에도 관심이 없다면, 모든 term에 대해 일치하는 document만 index하도록 설정할 수 있다. 여전히 이 field를 search할 수 있지만, phrase query는 error가 발생하고, score는 term은 모든 document에 한 번씩만 나타나는 것으로 가정한다.

PUT index
{
  "mappings": {
    "type": {
      "properties": {
        "foo": {
          "type": "text",
          "norms": false,
          "index_options": "freqs"
        }
      }
    }
  }
}

Don’t use default dynamic string mappings

The default dynamic string mappings will index string fields both as text and keyword. This is wasteful if you only need one of them. Typically an id field will only need to be indexed as a keyword while a body field will only need to be indexed as a text field.

기본 dynamic string mappings 는 string field는 text와 keyword 모두로 index한다. 이 중 하나만 필요하다면 낭비다. 일반적으로 id field는 keyword 로만 index되어야 하지만, body field는 오직 text field로만 index되어야 한다.

This can be disabled by either configuring explicit mappings on string fields or setting up dynamic templates that will map string fields as either text or keyword.

이는 string field를 명시적으로 mapping하거나 string field를 text 나 keyword 로 mapping하는 동적 template을 설정하여 비활성화할 수 있다.

For instance, here is a template that can be used in order to only map string fields as keyword:

예를 들어, 다음은 string field를 keyword 로만 mapping하기 위하여 사용된 template이다.

PUT index
{
  "mappings": {
    "type": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ]
    }
  }
}

Watch your shard size

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 creating indices with less primary shards, creating less indices (e.g. by leveraging the Rollover API), or modifying an existing index using the Shrink API.

shard가 클수록 data를 저장하는ㄷ데 더 효율적일 것이다. shard의 크기를 증가시키기 위해서는 더 적은 primary shard를 가진 index를 생성하여 index의 primary shard의 수를 감소시킬 수 있다. 또는 Rollover API 등을 활용하여 더 적은 index를 생성하거나 Shrink API 를 활용하여 기존의 index를 변경할 수 있다.

Keep in mind that large shard sizes come with drawbacks, such as long full recovery times.

큰 shard에는 전체 recovery 시간 같은 단점이 있다는 것을 명심하자.

Disable _all

The _all field indexes the value of all fields of a document and can use significant space. If you never need to search against all fields at the same time, it can be disabled.

_all field는 document의 모든 field의 값을 index하며 상당한 공간을 사용할 수 있다. 동시에 모든 field에 대해 search할 필요가 없다면, 비활성화할 수 있다.

Disable _source

The _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.

_source field는 document의 원래 JSON body를 저장한다. 그 값을 access할 필요가 없다면 비활성화할 수 있다. 그러나, _source 를 access해야 하는 update, reindex 같은 API는 동작하지 않는다.

Use best_compression

The _source and stored fields can easily take a non negligible amount of disk space. They can be compressed more aggressively by using the best_compression codec.

_source 와 stored field는 무시할 수 없는 양의 디스크 공간을 차지한다. best_compression codec 을 사용하여 이들을 보다 공격적으로 압축할 수 있다.

Force Merge

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.

Elasticsearch의 index는 하나 이상의 shard에 저장된다. 각 shard는 Lucene의 index이며 하나 이상의 segment(disk의 실제 file)로 구성된다. 더 큰 segment는 data를 저장하기가 더 효율적이다.

The _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.

_forcemerge API 는 shard별로 segment의 수를 줄이는데 사용될 수 있다. 대부분의 경우, segment의 수는 max_num_segments=1 을 설정하여 shard 당 1개로 줄일 수 있다. 

Shrink Index

The 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.

Shrink API 를 이용하여 index에서 shard의 수를 줄일 수 있다. 위의 _forcemerge API와 함께, index의 shard와 segment 수를 크게 줄일 수 있다.

Use the smallest numeric type that is sufficient

The type that you pick for numeric data can have a significant impact on disk usage. In particular, integers should be stored using an integer type (byteshortinteger or long) and floating points should either be stored in a scaled_float if appropriate or in the smallest type that fits the use-case: using float over double, or half_float over float will help save storage.

numeric data 의 type은 disk 사용량에 상당한 영향을 줄 수 있다. 특히, integer는 integer type( byteshortinteger or long )으로 저장되어야 하며, 부동 소수는 적절한 scaled_float 이나 가장 작은 type으로 저장되어야 한다.

Use index sorting to colocate similar documents

When Elasticsearch stores _source, it compresses multiple documents at once in order to improve the overall compression ratio. For instance it is very common that documents share the same field names, and quite common that they share some field values, especially on fields that have a low cardinality or a zipfian distribution.

Elasticsearch가 _so를 저장하면, 전체 압축 비율을 높이기 위해 여러 document를 한번에 압축한다. 예를 들어, document가 동일한 field 이름을 공유하는 것은 매우 일반적이며, 특히 낮은 cardinality나 zipfian 분포를 가지는 field의 값을 공유하는 것은 꽤 흔하다.

By default documents are compressed together in the order that they are added to the index. If you enabled index sorting then instead they are compressed in sorted order. Sorting documents with similar structure, fields, and values together should improve the compression ratio.

기본적으로 document는 index에 추가된 순서대로 함께 압축된다. index sorting 을 활성화했다면, 대신 정렬된 순서대로 압축된다. 유사한 구조, field, 값을 가진 document를 함께 정렬하면, 압축 비율이 향상된다. 

Put fields in the same order in documents

Due to the fact that multiple documents are compressed together into blocks, it is more likely to find longer duplicate strings in those _source documents if fields always occur in the same order.

여러 document가 block으로 함께 압축되기 때문에, field가 항상 동일한 순서로 발생할 경우, _source document에서 더 긴 문자열이 발견될 가능성이 더 높다.

'Reference > How To ...' 카테고리의 다른 글

5. Tune for search speed  (0) 2018.10.05
4. Tune for indexing speed  (0) 2018.10.05
3. Getting consistent scoring  (0) 2018.10.05
2. Mixing exact search with stemming  (0) 2018.10.05
1. General recommendations  (0) 2018.10.05