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函数,即可解决
相关推荐
Elasticsearch5 小时前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
Elasticsearch1 天前
通用表达式语言 ( CEL ): CEL 输入如何改进 Elastic Agent 集成中的数据收集
elasticsearch
武子康1 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康2 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP3 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库3 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟3 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体
B站计算机毕业设计超人3 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法
计算机程序猿学长3 天前
大数据毕业设计-基于django的音乐网站数据分析管理系统的设计与实现(源码+LW+部署文档+全bao+远程调试+代码讲解等)
大数据·django·课程设计
B站计算机毕业设计超人3 天前
计算机毕业设计Django+Vue.js音乐推荐系统 音乐可视化 大数据毕业设计 (源码+文档+PPT+讲解)
大数据·vue.js·hadoop·python·spark·django·课程设计