Elasticsearch聚合 之 Date Histogram聚合

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介:

Elasticsearch的聚合主要分成两大类:metric和bucket,2.0中新增了pipeline还没有研究。本篇还是来介绍Bucket聚合中的常用聚合——date histogram.参考:官方文档

用法

Date histogram的用法与histogram差不多,只不过区间上支持了日期的表达式。

{
"aggs":{
    "articles_over_time":{
        "date_histogram":{
            "field":"date",
            "interval":"month"
            }
        }
    }
}

interval字段支持多种关键字:`year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`

当然也支持对这些关键字进行扩展使用,比如一个半小时可以定义成如下:

{
    "aggs":{
        "articles_over_time":{
            "date_histogram":{
                "field":"date",
                "interval":"1.5h"
                }
            }
        }
}

返回的结果可以通过设置format进行格式化:

{
    "aggs":{
        "articles_over_time":{
            "date_histogram":{
                "field":"date",
                "interval":"1M",
                "format":"yyyy-MM-dd"
                }
            }
        }
    }

得到的结果如下:

{
    "aggregations":{
        "articles_over_time":{
            "buckets":[{
                "key_as_string":"2013-02-02",
                "key":1328140800000,
                "doc_count":1
            },{
                "key_as_string":"2013-03-02",
                "key":1330646400000,
                "doc_count":2
            },
            ...
            ]}
        }
}

其中key_as_string是格式化后的日期,key显示了是日期时间戳,

time_zone时区的用法

在es中日期支持时区的表示方法,这样就相当于东八区的时间。

{
    "aggs":{
        "by_day":{
            "date_histogram":{
                "field":"date",
                "interval":"day",
                "time_zone":"+08:00"
            }
        }
    }
}

offset 使用偏移值,改变时间区间

默认情况是从凌晨0点到午夜24:00,如果想改变时间区间,可以通过下面的方式,设置偏移值:

{"aggs":{
    "by_day":{
        "date_histogram":{
            "field":"date",
            "interval":"day",
            "offset":"+6h"
            }
        }
    }
}

那么桶的区间就改变为:

"aggregations":{
    "by_day":{
        "buckets":[{
            "key_as_string":"2015-09-30T06:00:00.000Z",
            "key":1443592800000,
            "doc_count":1
        },{
            "key_as_string":"2015-10-01T06:00:00.000Z",
            "key":1443679200000,
            "doc_count":1
        }]
    }
}

Missing Value缺省字段

当遇到没有值的字段,就会按照缺省字段missing value来计算:

{
    "aggs":{
        "publish_date":{
            "date_histogram":{
                "field":"publish_date",
                "interval":"year",
                "missing":"2000-01-01"
            }
        }
    }
}

其他

对于其他的一些用法,这里就不过多赘述了,比如脚本、Order、min_doc_count过滤,extended_bounds等都是支持的。

本文转自博客园xingoo的博客,原文链接: Elasticsearch聚合 之 Date Histogram聚合,如需转载请自行联系原博主。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
存储 搜索推荐 Java
|
6月前
|
存储 人工智能 自然语言处理
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
|
2月前
|
存储 SQL Java
聚合在Elasticsearch中的使用及示例验证
聚合在Elasticsearch中的使用及示例验证
71 0
|
2月前
|
测试技术 定位技术 API
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
94610 140
|
2月前
|
iOS开发 索引 MacOS
Elasticsearch 聚合字段aggregate-metric-double
Elasticsearch 聚合字段aggregate-metric-double
22 0
|
2月前
|
缓存 Java API
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——聚合与搜索(三)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——聚合与搜索(三)
|
4月前
|
数据库 Python
Python-ElasticSearch客户端的封装(聚合查询、统计查询、全量数据)
Python-ElasticSearch客户端的封装(聚合查询、统计查询、全量数据)
54 0
|
15天前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
27天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
113 0
|
2月前
|
存储 监控 搜索推荐
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)

热门文章

最新文章