ES doc_values介绍——本质是field value的列存储,做聚合分析用,ES默认开启,会占用存储空间(列存储压缩技巧,除公共除数或者同时减去最小数,字符串压缩的话,直接去重后用数字ID压

大家知道,搜索引擎的基本数据结构是反向索引,也就是为每个关键词建立了到文档的映射,然后所有的关键词是一个有序列表。搜索的时候,只要先从有序列表中匹配到关键词,就能搜索到包含该关键词的所有文档,反向索引的数据结构对于关键词搜索的场景是非常高效的。


但聚合分析和搜索有很大的不同。典型的场景,比如计算某个文档中每个关键词的出现次数,反向索引就无能为力了,需要先扫描整个关键词映射表,才能找到该文档包含的所有关键词,然后再进行聚合统计(这个例子其实不太准确,因为Lucene在反向索引中冗余了词频的信息,用于计算搜索相关度),也就是要对整个反向索引做全扫描,在数据量大的时候,性能当然好不到哪里去。

所以后来又引入了一个新的机制,就是DocValues,DocValues是持久化存储在文件中,并且是预先构建的,也就是数据进入到Elasticsearch时,就会同时生成反向索引和DocValues




doc_values

Doc values are the on-disk data structure, built at document index time, which makes this data access pattern possible. They store the same values as the _source but in a column-oriented fashion that is way more efficient for sorting and aggregations.(本质!!!) Doc values are supported on almost all field types, with the notable exception of analyzed string fields.

All fields which support doc values have them enabled by default. If you are sure that you don’t need to sort or aggregate on a field, or access the field value from a script, you can disable doc values in order to save disk space:

PUT my_index
{
  "mappings": { "my_type": { "properties": { "status_code": {  "type": "keyword" }, "session_id": {  "type": "keyword", "doc_values": false } } } } }
 

The status_code field has doc_values enabled by default.

The session_id has doc_values disabled, but can still be queried.

 简单的说,Elasticsearch通过反向索引做搜索,通过DocValues列式存储做分析,将搜索和分析的场景统一到了通一个分布式系统中,还是很有搞头的。不过分析不仅仅是聚合,这也是Elasticsearch还需要继续努力的方向,目前通过Elasticsearch-Hadoop项目,可以将Elasticsearch的搜索结果做为Spark的RDD,利用Spark做更深度的分析。未来如果分布式计算这一层能够和Spark这样的计算框架再进一步做深度的融合,恐怕有可能成为大数据领域内的另外一个大杀器。

摘自:https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html

你可能感兴趣的:(elasticsearch)