ElasticSearch:使用dsl语句同时查询出最近2小时、最近1天、最近7天、最近30天的数量

场景

需要使用dsl语句同时查询出最近2小时、最近1天、最近7天、最近30天的数量,如果按照常规逻辑,需要写四个dsl语句,才能查询出来,那么能不能使用一句dsl就可以查询出结果呢?

show time

xml 复制代码
GET alarm_forward_history_*/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "sourceName": {
              "value": "运维123"
            }
          }
        },
        {
          "term": {
            "sourceAlertKey": {
              "value": "生产问题描述223"
            }
          }
        },
        {
          "term": {
            "cmdb.app_name": {
              "value": "dba数据库"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "date": {
      "date_range": {
        "field": "@timestamp",
        "ranges": [
          {
            "from": "now-2h",
            "to": "now",
            "key": "lastTwoHour"
          },
          {
            "from": "now-1d",
            "to": "now",
            "key": "lastDay"
          },
          {
            "from": "now-7d",
            "to": "now",
            "key": "lastSevenDay"
          },
          {
            "from": "now-30d",
            "to": "now",
            "key": "lastMonth"
          }
        ]
      }
    }
  }
}

输出结果大致如下:

xml 复制代码
"aggregations" : {
    "date" : {
      "buckets" : [
        {
          "key" : "lastMonth",
          "from" : 1.728359639296E12,
          "from_as_string" : "2024-10-08T03:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 7
        },
        {
          "key" : "lastSevenDay",
          "from" : 1.730346839296E12,
          "from_as_string" : "2024-10-31T03:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 0
        },
        {
          "key" : "lastDay",
          "from" : 1.730865239296E12,
          "from_as_string" : "2024-11-06T03:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 0
        },
        {
          "key" : "lastTwoHour",
          "from" : 1.730944439296E12,
          "from_as_string" : "2024-11-07T01:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 0
        }
      ]
    }
  }
}

总结

  • 直接采用date_range函数,即可解决
相关推荐
V搜xhliang02467 小时前
机器人建模(URDF)与仿真配置
大数据·人工智能·深度学习·机器学习·自然语言处理·机器人
房产中介行业研习社7 小时前
2026年3月哪些房源管理系统功能全
大数据·运维·人工智能
玄微云8 小时前
2026年通用软件难适配,垂直店务系统反而更省心
大数据·云计算·软件需求
Elastic 中国社区官方博客9 小时前
Elastic 为什么捐赠其 OpenTelemetry PHP 发行版
大数据·开发语言·elasticsearch·搜索引擎·信息可视化·全文检索·php
方向研究9 小时前
ABS生产
大数据
TDengine (老段)10 小时前
TDengine 视图功能使用
大数据·数据库·servlet·时序数据库·tdengine·涛思数据
TDengine (老段)10 小时前
TDengine IDMP 运维指南 —— 部署架构
大数据·运维·数据库·架构·时序数据库·tdengine·涛思数据
utmhikari10 小时前
【测试人生】变更规则校验Agent研发的一些思路
大数据·人工智能·llm·agent·变更风险·openclaw
AC赳赳老秦10 小时前
DeepSeek优化多智能体指令:避免协同冲突,提升自动化流程稳定性
android·大数据·运维·人工智能·自然语言处理·自动化·deepseek
成长之路51411 小时前
【数据集】A股上市公司数字投资数据集-含代码(2000-2024年)
大数据