用short代替long是否有助于减少Elasticsearch的磁盘需求



我正在努力降低存储在ElasticSearch中的数据的磁盘需求。数据主要是包含浮点值列表的字段。我正在从流作业生成json数据,然后将其放入Elasticsearch。将数据类型从float更改为short是否有助于减少磁盘需求(请记住,值的范围在短范围内,我们可以对浮点值进行四舍五入。(

我有个医生:https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html但这里有相互矛盾的说法——

  1. 存储是根据存储的实际值进行优化的,因此选择一种类型而不是另一种类型不会对存储需求产生影响
  2. 这在很大程度上有助于节省磁盘空间,因为整数比浮点更容易压缩

有人能帮忙解释吗?

浮点值是32位IEEE 754浮点数,短值是16位整数,因此很明显,短值需要更少的磁盘空间。

您提到的第一条语句仅对整数类型有效,而第二条语句是关于存储为整数的浮点数的,所以这有点像比较苹果和桔子。

但是,对于您的具体情况,当您的值在短范围内时,让我们进行一个非常天真但经验丰富的测试,我们生成大量随机短值(比如1M(,并将它们存储在两个不同的索引中,一个索引的字段映射为short,另一个索引字段映射为float,然后简单地比较它们的大小。

以下是我使用过的映射:

PUT shorts 
{
"mappings": {
"properties": {
"my_short": {
"type": "short"
}
}
}
}
PUT floats 
{
"mappings": {
"properties": {
"my_float": {
"type": "float"
}
}
}
}

下面是存储在其中的随机值:

{"index":{"_index":"shorts"}}
{"my_short":1799}
{"index":{"_index":"floats"}}
{"my_float":1799}
{"index":{"_index":"shorts"}}
{"my_short":31014}
{"index":{"_index":"floats"}}
{"my_float":31014}
{"index":{"_index":"shorts"}}
{"my_short":-880}
{"index":{"_index":"floats"}}
{"my_float":-880}
{"index":{"_index":"shorts"}}
{"my_short":31159}
{"index":{"_index":"floats"}}
{"my_float":31159}
...

加载所有数据后,我们可以检查索引的相应大小

GET _cat/indices/shorts,floats?v
health status index  uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   floats puHwIu5wSSG23QEq4qxROA   1   1    1000000            0     57.7mb         28.8mb
green  open   shorts mDEHUB3FQoyuMNbsDy3zwA   1   1    1000000            0     53.3mb         26.6mb

所以答案很清楚:对于完全相同的数据,浮点值占用更多的空间

最新更新