首页 > 解决方案 > 聚合任务如何从 Elasticsearch 中的倒排索引中受益

问题描述

我知道搜索任务可以从 Elasticsearch 中的倒排索引中受益。

但我不明白聚合任务如何从倒排索引中受益。例如,如果我们有以下文档

id, name,   gender, age, weight
1,  Tom,    m,      29,  100
2,  James,  m,      28,  120
3,  Lucy,   f,      27,  80
4,  Kevin,  m,      28,  150
5,  Jessica,f,      22,  100
....

如果我想获得年龄 = 28 的平均体重,使用倒排索引弹性搜索的步骤应该是

1. get the doc list of age = 28 which may looks like [id=2, id=4 ...]
2. Read each doc to get the weight
3. Add the weight and divide by the number of records

这似乎效率不高,尤其是对于第 2 步。由于磁盘上的文档位置不连续,因此 Elasticsearch 无法在一次读取中加载数据,因此需要读取很多次。

那么为什么 Elasticsearch 在聚合上能有这么好的表现呢?它是否使用除了倒排索引之外的其他数据结构进行聚合?我对聚合步骤的理解是否错误?

标签: elasticsearchelasticsearch-5elasticsearch-aggregationinverted-index

解决方案


这里的关键是要了解倒排索引和其他相关的 Lucene 文件不是在磁盘上访问,而是由 Lucene映射到内存(不是在堆上!)。

因此,无需赘述,这基本上就是 ES 如何在搜索和聚合方面实现出色性能的方式。


推荐阅读