【Elasticsearch】date range聚合

好的,继续之前的示例:

```json

]

}

}

}

}

```

4.3 自定义键(`key`)

通过为每个范围指定一个唯一的键(`key`),可以在结果中更方便地引用每个范围。这在使用`keyed`参数将结果以键值对形式返回时尤其有用。

```json

POST /sales/_search?size=0

{

"aggs": {

"sales_range": {

"date_range": {

"field": "date",

"ranges": [

{ "to": "2024-01-01", "key": "before_2024" },

{ "from": "2024-01-01", "key": "after_2024" }

],

"keyed": true

}

}

}

}

```

响应示例:

```json

{

"aggregations": {

"sales_range": {

"buckets": {

"before_2024": {

"to": 1640995200000,

"to_as_string": "2024-01-01",

"doc_count": 10

},

"after_2024": {

"from": 1640995200000,

"from_as_string": "2024-01-01",

"doc_count": 20

}

}

}

}

}

```

5.常见用例

5.1 按时间段分析数据

`date_range`聚合非常适合按时间段分析数据,例如按月、按季度或按年分析销售数据、用户活动等。

示例:按季度分析销售数据

假设你想按季度分析2024年的销售数据:

```json

POST /sales/_search?size=0

{

"aggs": {

"quarterly_sales": {

"date_range": {

"field": "date",

"format": "yyyy-MM-dd",

"ranges": [

{ "from": "2024-01-01", "to": "2024-04-01", "key": "Q1" },

{ "from": "2024-04-01", "to": "2024-07-01", "key": "Q2" },

{ "from": "2024-07-01", "to": "2024-10-01", "key": "Q3" },

{ "from": "2024-10-01", "to": "2025-01-01", "key": "Q4" }

]

}

}

}

}

```

响应示例:

```json

{

"aggregations": {

"quarterly_sales": {

"buckets": [

{

"key": "Q1",

"from": 1640995200000,

"from_as_string": "2024-01-01",

"to": 1651382400000,

"to_as_string": "2024-04-01",

"doc_count": 30

},

{

"key": "Q2",

"from": 1651382400000,

"from_as_string": "2024-04-01",

"to": 1656662400000,

"to_as_string": "2024-07-01",

"doc_count": 40

},

{

"key": "Q3",

"from": 1656662400000,

"from_as_string": "2024-07-01",

"to": 1664592000000,

"to_as_string": "2024-10-01",

"doc_count": 50

},

{

"key": "Q4",

"from": 1664592000000,

"from_as_string": "2024-10-01",

"to": 1672531200000,

"to_as_string": "2025-01-01",

"doc_count": 60

}

]

}

}

}

```

5.2 处理时区差异

在处理跨时区的数据时,`time_zone`参数非常有用。例如,如果你的数据存储在 UTC,但需要按用户所在时区进行分析,可以使用`time_zone`参数。

示例:按美国东部时间(EST)分析日志数据

假设你有一个日志索引`logs`,其中的`timestamp`字段记录了日志的时间戳(以 UTC 存储)。你希望按美国东部时间(EST,UTC-5)分析每天的日志数量:

```json

POST /logs/_search?size=0

{

"aggs": {

"daily_logs": {

"date_histogram": {

"field": "timestamp",

"calendar_interval": "1d",

"time_zone": "America/New_York"

}

}

}

}

```

5.3 动态日期范围

使用日期数学表达式可以动态定义日期范围。例如,你可以根据当前时间动态生成过去几个月的范围。

示例:动态生成过去12个月的范围

假设你想分析过去12个月的销售数据:

```json

POST /sales/_search?size=0

{

"aggs": {

"monthly_sales": {

"date_range": {

"field": "date",

"format": "yyyy-MM-dd",

"ranges": [

{ "from": "now-12M/M", "to": "now-11M/M", "key": "12_months_ago" },

{ "from": "now-11M/M", "to": "now-10M/M", "key": "11_months_ago" },

{ "from": "now-10M/M", "to": "now-9M/M", "key": "10_months_ago" },

{ "from": "now-9M/M", "to": "now-8M/M", "key": "9_months_ago" },

{ "from": "now-8M/M", "to": "now-7M/M", "key": "8_months_ago" },

{ "from": "now-7M/M", "to": "now-6M/M", "key": "7_months_ago" },

{ "from": "now-6M/M", "to": "now-5M/M", "key": "6_months_ago" },

{ "from": "now-5M/M", "to": "now-4M/M", "key": "5_months_ago" },

{ "from": "now-4M/M", "to": "now-3M/M", "key": "4_months_ago" },

{ "from": "now-3M/M", "to": "now-2M/M", "key": "3_months_ago" },

{ "from": "now-2M/M", "to": "now-1M/M", "key": "2_months_ago" },

{ "from": "now-1M/M", "to": "now/M", "key": "1_month_ago" }

]

}

}

}

}

```

6.注意事项

6.1 日期格式

确保`from`和`to`的日期格式与`format`参数一致。如果未指定`format`,Elasticsearch 将使用默认的日期格式。

6.2 时区转换

在使用`time_zone`参数时,Elasticsearch 会将所有日期字段从 UTC 转换为指定的时区。这可能会影响日期的四舍五入行为。

6.3 性能优化

虽然`date_range`聚合在 Elasticsearch 中是高效的,但在处理大量数据时,仍需注意性能影响。可以通过以下方式优化性能:

• 索引优化:确保日期字段已正确索引。

• 查询优化:尽量减少返回的字段数量,使用`size=0`来避免返回文档内容。

• 缓存:对于频繁查询的范围,可以考虑使用缓存。

7.总结

`date_range`聚合是 Elasticsearch 中一个非常强大的工具,用于将日期数据

相关推荐
Albert.H.Holmes1 小时前
Elasticsearch学习
大数据·学习·elasticsearch
Biehmltym17 小时前
【AI】09AI Agent LLM → Streaming → Session 记录 的完整链路
大数据·人工智能·elasticsearch
小湘西18 小时前
Elasticsearch 的一些默认配置上下限
java·大数据·elasticsearch
Dxy123931021621 小时前
Elasticsearch 8如何做好标题搜索
大数据·elasticsearch
斯普信云原生组1 天前
Elasticsearch(ES) 内存 CPU 过高问题排查报告
大数据·elasticsearch·搜索引擎
弘毅 失败的 mian1 天前
Git 分支管理
大数据·经验分享·笔记·git·elasticsearch
阿坤带你走近大数据1 天前
Elasticsearch(ES)的基本概念、架构及基本使用介绍
大数据·elasticsearch
Elastic 中国社区官方博客1 天前
使用 Elasticsearch 中的结构化输出创建可靠的 agents
大数据·人工智能·elk·elasticsearch·搜索引擎·ai·全文检索
G皮T1 天前
【Elasticsearch】查询性能调优(六):track_total_hits 影响返回结果的相关性排序吗
大数据·数据库·elasticsearch·搜索引擎·全文检索·性能·opensearch
LCG米1 天前
嵌入式Linux系统构建:为STM32MP157移植Buildroot并开发温湿度采集驱动
linux·stm32·elasticsearch